I am in Android Studio, and I am trying to create an Android app that can do some networking. I can get some JSON data, but whenever I convert the String that I get to a JSONObject, a NullPointerException is thrown. Here is my Log error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:156)
at org.json.JSONObject.<init>(JSONObject.java:173)
at com.daita.getdusa.GetDataTask.doInBackground(GetDataTask.java:58)
at com.daita.getdusa.GetDataTask.doInBackground(GetDataTask.java:21)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
The line of code that is causing the exception is this:
JSONObject object = new JSONObject(result);
Edit
I was asked to put more source code, so here it is(The AsyncTask)
public class GetDataTask extends AsyncTask<Void, Void, String> {
TextView submitView;
public GetDataTask(TextView submitView){
this.submitView = submitView;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
submitView.setText("Loading...");
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
submitView.setText(s);
}
#Override
protected String doInBackground(Void... voids) {
String result;
result = getJSON("http://api.datausa.io/attrs/geo/");
JSONArray data;
List<String> parsedData = null;
List<String> geoID = new ArrayList<>();
try {
JSONObject jsonObject = new JSONObject(result);
data = jsonObject.getJSONArray("data");
parsedData = new ArrayList<>();
Log.i("DUSA", String.valueOf(jsonObject.length()));
for(int i=0; i < data.length(); i++){
parsedData.add(data.getString(i));
}
for(String string: parsedData){
//8, 9
String[] dta = string.split(",");
String populationResult = getJSON("http://api.datausa.io/api/?show=geo&sumlevel=all&required=pop&year=latest&geo="+dta[9].substring(1, dta[9].length()-1));
if (result != null && result != "{\"error\": \"No tables can match the specified query.\"}") {
JSONObject popObject = new JSONObject(populationResult);
JSONArray popData = popObject.getJSONArray("data");
String section = popData.getString(0);
geoID.add(dta[8] + "," + dta[9] + "," + section);
} else{
geoID.add(dta[8] + "," + dta[9] + ", null");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return geoID.toString();
}
public String getJSON(String url) {
HttpURLConnection c = null;
try {
URL u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.connect();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
}
} catch (Exception ex) {
return ex.toString();
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
}
}
}
return null;
}
}
Note: If you want real time source code updates, look at these files https://www.dropbox.com/sh/5852rt2rakk6iii/AAAgjsqOTsFfP1N4kS1KzuTBa?dl=0
Trying to do it all for you, but let me go step by step to make it simpler for you:
package com.daita.getdusa;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by User on 8/12/2016.
*/
public class GetDataTask extends AsyncTask<String, Void, Void>{
// Put in Main never here.
String result;
String allValues;
Context context;
public GetDataTask(Context mContext){
this.context = mContext;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
((TextView)((MainActivity)context).findViewById(R.id.resultView)).setText("Loading...");
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
((TextView)((MainActivity)context).findViewById(R.id.resultView)).setText(result);
}
#Override
protected Void doInBackground(String... strings) {
HttpURLConnection c = null;
try {
URL u = new URL("http://api.datausa.io/attrs/geo/"); // use urls first value
c = (HttpURLConnection) u.openConnection();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
result = sb.toString();
getValue();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
}
}
}
return null;
}
public void getValue() {
// FIX IT
// Parse the JSON properly
}
}
Now learn about json parsing, the data is huge, you need the response in smaller format this huge json, even crashes online json formatter. :)
This code works to show all of the json. Hope this helps.
public class Test {
static String Json="{'root':{['ratedetails':{['levelOne':{["+ "{'FGROUP':'A','rentalpackage':'2D','NOOFDAYS':'2', 'HOURS':'0', 'PERIODPRICE':'100.00',"+ "'CUSTOMERPERDAY':'50.00', 'DRIVERPERDAY':'50.00' ,'KMALLOWANCE':'2', 'EXTKMALLOWPDAY':'60',"+ "'DRIVERKMCHARGE':'5', 'CUSTOMERKMCHARGE':'3', 'MAXIMUMKMCHARGE':'99999999', 'MAXKMCHRGTYPE':'0'"+",'HIDDEN':'2'},"+ "{'FGROUP':'A','rentalpackage':'2D','NOOFDAYS':'2', 'HOURS':'0', 'PERIODPRICE':'100.00',"+ "'CUSTOMERPERDAY':'50.00', 'DRIVERPERDAY':'50.00','KMALLOWANCE':'2', 'EXTKMALLOWPDAY':'60',"+ "'DRIVERKMCHARGE':'5', 'CUSTOMERKMCHARGE':'3', 'MAXIMUMKMCHARGE':'99999999', 'MAXKMCHRGTYPE':'0'"+",'HIDDEN':'2'}"+ "]}"+ "]}"+ "]}}";
public static void main(String[] args) {
//new Test().main();
String s[]=Json.split(",");
for(int i=0;i<s.length;i++)
{
if(true){
System.out.println(s[i].split(":")[s[i].split(":").length-2].substring(s[i].split(":")[s[i].split(":").length-2].indexOf("'")+1, s[i].split(":")[s[i].split(":").length-2].lastIndexOf("'"))+" "+ s[i].split(":")[s[i].split(":").length-1].substring(s[i].split(":")[s[i].split(":").length-1].indexOf("'")+1, s[i].split(":")[s[i].split(":").length-1].lastIndexOf("'")));
}
else{
System.out.println(s[i].split(":")[0].replace("'", "").trim()+" "+s[i].split(":")[1].replace("'", "").trim()); } } // System.out.println(Json.split(",")[5].split(":")[1].replace("'", "")); } }
Related
I have 4 large files (around 1.5 gb each) and I want to process these files, read each line of the file and convert it to a customer object. I have the following implementation.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import java.util.zip.GZIPInputStream;
import static java.nio.charset.StandardCharsets.UTF_8;
public class CustomerDataAccess {
public static void main(String[] args) throws IOException {
CustomerFileItem john = new CustomerFileItem("CustFile1", "http://w.customer1.com");
CustomerFileItem sarah = new CustomerFileItem("CustFile2", "http://w.customer2.com");
CustomerFileItem charles = new CustomerFileItem("CustFile3", "http://w.customer3.com");
List<CustomerFileItem> customers = Arrays.asList(john, sarah, charles);
Iterator<CustomerFileLineItem> custList = new CustIterator(customers);
}
public static class CustIterator implements Iterator<CustomerFileLineItem> {
private static final int HEADER_LINES = 9; // 8 + 1 blank line
BufferedReader bufferedReader;
private int index = 0;
private final List<CustomerFileItem> custFileItems = new ArrayList<>();
public CustIterator(final List<CustomerFileItem> custFileItems) throws IOException {
this.custFileItems.addAll(custFileItems);
processNext();
}
private void processNext() throws IOException {
if (bufferedReader != null) {
bufferedReader.close();
}
if (index < custFileItems.size()) { // only update if there's another file
CustomerFileItem custFileItem = custFileItems.get(index);
GZIPInputStream gis = new GZIPInputStream(new URL(custFileItem.url).openStream());
// default buffer size is 8 KB
bufferedReader = new BufferedReader(new InputStreamReader(gis, UTF_8));
// read the first few lines
for (int i = 0; i < HEADER_LINES; i++) {
bufferedReader.readLine();
}
}
index++;
}
#Override
public boolean hasNext() {
try {
boolean currentReaderStatus = bufferedReader.ready();
if (currentReaderStatus) {
return true;
} else if (index < custFileItems.size()) {
// at end of current file, try to get the next one
processNext();
return hasNext();
} else { // no more files left
return false;
}
} catch (IOException e) {
try {
bufferedReader.close();
} catch (IOException e1) {
throw new UncheckedIOException(e1);
}
throw new UncheckedIOException(e);
}
}
#Override
public CustomerFileLineItem next() {
try {
String line = bufferedReader.readLine();
if (line != null) {
return new CustomerFileLineItem(line);
} else {
return null;
}
} catch (IllegalArgumentException exception) {
return null;
} catch (IOException e) {
try {
bufferedReader.close();
} catch (IOException e1) {
throw new UncheckedIOException(e1);
}
throw new UncheckedIOException(e);
}
}
#Override
public void remove() {
throw new UnsupportedOperationException();
}
#Override
public void forEachRemaining(final Consumer<? super CustomerFileLineItem> action) {
throw new UnsupportedOperationException();
}
}
public static class CustomerFileLineItem {
private static final int NUMBER_OF_FIELDS = 4;
final String id;
final String productNumber;
final String usageType;
final String operation;
public CustomerFileLineItem(final String line) {
String[] strings = line.split(",");
if (strings.length != NUMBER_OF_FIELDS) {
throw new IllegalArgumentException(String.format("Malformed customer file line: %s", line));
}
this.id = strings[0];
this.productNumber = strings[1];
this.usageType = strings[3];
this.operation = strings[4];
}
}
static class CustomerFileItem {
private String fileName;
private String url;
public CustomerFileItem(String fileName, String url) {
this.fileName = fileName;
this.url = url;
}
}
}
In one of use case I want use streams in the output list(custList). But I know I can't use streams with Iterator. How I can convert it to Spliterator? Or how can I implement the same that I implement with Iterator in Spliterator?
TL;DR You don’t need to implement an Iterator or Spliterator, you can simply use a Stream in the first place:
private static final int HEADER_LINES = 9; // 8 + 1 blank line
Stream<CustomerFileLineItem> stream = customers.stream()
.flatMap(custFileItem -> {
try {
GZIPInputStream gis
= new GZIPInputStream(new URL(custFileItem.url).openStream());
BufferedReader br = new BufferedReader(new InputStreamReader(gis, UTF_8));
// read the first few lines
for (int i = 0; i < HEADER_LINES; i++) br.readLine();
return br.lines().onClose(() -> {
try { br.close(); }
catch(IOException ex) { throw new UncheckedIOException(ex); }
});
} catch(IOException ex) {
throw new UncheckedIOException(ex);
}
})
.map(CustomerFileLineItem::new);
But for completeness, addressing the question literally:
First of all, you should not add a method definition like
#Override
public void forEachRemaining(final Consumer<? super CustomerFileLineItem> action) {
throw new UnsupportedOperationException();
}
This method will surely backfire when you use the Stream API, as that’s where most non-short-circuiting operations will end up.
There is not even a reason to add it. When you don’t declare the method, you’ll get a reasonable default method from the Iterator interface.
When you fixed this issue, you can easily convert the Iterator to a Spliterator using Spliterators.pliteratorUnknownSize(Iterator, int).
But there is no reason to do so. Your code becomes simpler when implementing Spliterator in the first place:
public static class CustIterator
extends Spliterators.AbstractSpliterator<CustomerFileLineItem> {
private static final int HEADER_LINES = 9; // 8 + 1 blank line
BufferedReader bufferedReader;
private final ArrayDeque<CustomerFileItem> custFileItems;
public CustIterator(final List<CustomerFileItem> custFileItems) throws IOException {
super(Long.MAX_VALUE, ORDERED|NONNULL);
this.custFileItems = new ArrayDeque<>(custFileItems);
processNext();
}
#Override
public boolean tryAdvance(Consumer<? super CustomerFileLineItem> action) {
if(bufferedReader == null) return false;
try {
String line = bufferedReader.readLine();
while(line == null) {
processNext();
if(bufferedReader == null) return false;
line = bufferedReader.readLine();
}
action.accept(new CustomerFileLineItem(line));
return true;
}
catch(IOException ex) {
if(bufferedReader != null) try {
bufferedReader.close();
bufferedReader = null;
}
catch(IOException ex2) {
ex.addSuppressed(ex2);
}
throw new UncheckedIOException(ex);
}
}
private void processNext() throws IOException {
if (bufferedReader != null) {
bufferedReader.close();
bufferedReader = null;
}
if (!custFileItems.isEmpty()) { // only update if there's another file
CustomerFileItem custFileItem = custFileItems.remove();
GZIPInputStream gis
= new GZIPInputStream(new URL(custFileItem.url).openStream());
// default buffer size is 8 KB
bufferedReader = new BufferedReader(new InputStreamReader(gis, UTF_8));
// read the first few lines
for (int i = 0; i < HEADER_LINES; i++) {
bufferedReader.readLine();
}
}
}
}
But, as said at the beginning, you don’t even need to implement a Spliterator here.
Every Iterable<T> object has the following methods:
Iterator<T> iterator() returning Iterator<T>
default Spliterator<T> spliterator() (default method) returning Spliterator<T>
Therefore, you want to create Iterable<T> back from Iterator<T> which requires to override the only one non-default and abstract method:
Iterable<CustomerFileLineItem> iterable = new Iterable<CustomerFileLineItem>() {
#Override
public Iterator<CustomerFileLineItem> iterator() {
return custList;
}
};
This can be shortened into a lambda expression resulting in:
Iterable<CustomerFileLineItem> iterable = () -> custList;
Spliterator<CustomerFileLineItem> spliterator = iterable.spliterator();
... so the Stream is easily to be created:
Stream<CustomerFileLineItem> stream = StreamSupport.stream(spliterator, false);
An error occurs when searching for a name using the application thus showing no results when searching for a name within the app.
Tried changing between JSONArray and JSONObject but a similar error occurs. The URL is correct and does show the data in JSON format.
package com.rjassi.service;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
public class CharacterSearchService extends AbstractService {
private String query;
private JSONObject results;
private JSONObject jsonObject;
public CharacterSearchService(String query) {
try {
this.query = URLEncoder.encode(query, "UTF-8");
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
public JSONObject getResults() {
return results;
}
//This method will run on a separate thread to the UI
#Override
public void run() {
URL url;
boolean error = false;
HttpsURLConnection httpsURLConnection = null;
StringBuilder result = new StringBuilder();
try {
url = new URL("https://www.moogleapi.com/api/v1/characters/search?name=" + query);
httpsURLConnection = (HttpsURLConnection) url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
//put the result string into a JSONObject
JSONArray jsonArray = jsonObject.getJSONArray("name");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.has("Response") && jsonObject.getString("Response").equals("False")) {
error = true;
} else {
results = jsonObject;
}
}
/*
If the JSONObject has a "Response" attribute and it equals false then
no results were found
if (jsonObject.has("Response") && jsonObject.getString("Response").equals("False")) {
error = true;
} else {
results = jsonObject.getJSONArray("Search");
}
*/
} catch (Exception ex) {
ex.printStackTrace();
results = null;
error = true;
} finally {
if (httpsURLConnection != null) {
httpsURLConnection.disconnect();
}
}
/*
Call the serviceCallComplete() method in the super class; AbstractService which
will then inform the listeners that the search is complete
*/
super.serviceCallComplete(error);
}
}
public void serviceComplete(AbstractService abstractService) {
if (!abstractService.hasError()) {
//Cast the AbstractService object passed into the method to a CharacterSearchService object
CharacterSearchService characterSearchService = (CharacterSearchService) abstractService;
//Create a string array that is the same as the results JSONArray
String[] result = new String[characterSearchService.getResults().length()];
//searchResults.clear();
/*
Loops through the JSONArray and get the name of each JSONObject it contains.
Store each name in string array.
*/
for (int i = 0; i < characterSearchService.getResults().length(); i++) {
try {
//Store each character result as a JSONObject in the ArrayList
//searchResults.add(characterSearchService.getResults().getJSONObject(i));
android.util.Log.i("sdfsf", characterSearchService.getResults().getJSONObject(String.valueOf(i)).getString("name"));
result[i] = characterSearchService.getResults().getJSONObject(String.valueOf(i)).getString("name");
} catch (JSONException ex) {
result[i] = "error";
}
}
//Display the string array on screen in the ListView.
setListAdapter(new ArrayAdapter<String>(this, R.layout.final_fantasy_list_cell, R.id.text, result));
}
else{
String[] result = new String[]{"No Results"};
setListAdapter(new ArrayAdapter<String>(this, R.layout.final_fantasy_list_cell, R.id.text, result));
}
}
This is the error which occurs after searching for a name:
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
at com.rjassi.service.CharacterSearchService.run(CharacterSearchService.java:53)
at java.lang.Thread.run(Thread.java:764)
The expected output should show the name which is searched by returning that name or similar ones from the search results of the API.
jsonObject is never initialized so it will throw a NullPointerException when you try to dereference it. Initialize the JSONObject with your server response data.
If your response string is properly formatted JSON, you can initialize the JSONObject using the following constructor:
JSONObject
Added in API level 1
public JSONObject (String json)
Creates a new JSONObject with name/value mappings from the JSON string.
//put the result string into a JSONObject
JSONArray jsonArray = jsonObject.getJSONArray("name");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.has("Response") && jsonObject.getString("Response").equals("False")) {
error = true;
} else {
results = jsonObject;
}
}
This question already has answers here:
How can I fix 'android.os.NetworkOnMainThreadException'?
(66 answers)
Closed 6 years ago.
The purpose of the class below is to get text from different articles of different news websites. The version below is designed for Android, but it throws a NetworkOnMainThread Exception when run. When I used an earlier version of this class, made specifically to run on a computer, it worked fine, but I'm not really sure how network I/O works on Android. I've seen some other answers to questions about this topic, but I don't understand why in Android the program throws an exception but on a desktop it works fine. Can anyone explain?
package com.example.user.helloworld;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class ArticleReceiver {
private ArrayList<Article> newsArticles = new ArrayList<>();
private ArrayList<String> newsLinks = new ArrayList<>();
public ArticleReceiver(int numArticles, String link) {
if (numArticles != 0) {
receiveNewsArticles(numArticles, link);
}else{
System.out.println("ERROR: numArticles request for " + link + " cannot equal 0.");
}
}
private void receiveNewsArticles(int numArticles, String urlAddress) {
URL rssUrl = null;
// if connected to Internet
if (true){//isInternetAvailable()) {
try {
// gather links
rssUrl = new URL(urlAddress);
BufferedReader in = new BufferedReader(new InputStreamReader(rssUrl.openStream()));
String line;
// fix bbc trash urls
if (urlAddress.equals(Main.BBC_URL)) {
numArticles++;
}
while ((line = in.readLine()) != null && newsLinks.size() <= numArticles) {
if (line.contains("<link>")) {
// find links through tags
int firstPos = line.indexOf("<link>");
String temp = line.substring(firstPos);
temp = temp.replace("<link>", "");
int lastPos = temp.indexOf("</link>");
temp = temp.substring(0, lastPos);
newsLinks.add(temp);
}
}
in.close();
// test if there are links and if there is remove first
// unnecessary
// link
if (!newsLinks.isEmpty()) {
if (urlAddress.equals(Main.BBC_URL)) {
newsLinks.remove(0);
newsLinks.remove(0);
}else if(urlAddress.equals(Main.CNN_URL) || urlAddress.equals(Main.FOX_URL) || urlAddress.equals(Main.ESPN_URL)){
newsLinks.remove(0);
}
} else {
System.out.println("ERROR: No Found Articles. Check If You Have Wifi.");
}
// gather articles from HTML "section" or "p" tag of article using Jsoup
for (String newsLink : newsLinks) {
// get webpage
Document doc = Jsoup.connect(newsLink).get();
// get article from different websites
String article = null;
if (urlAddress.equals(Main.FOX_URL)) {
Elements element = doc.select("p");
article = element.text();
} else if (urlAddress.equals(Main.CNN_URL)) {
Elements element = doc.select("section");
article = element.text();
} else if (urlAddress.equals(Main.BBC_URL)) {
Elements element = doc.select("p");
article = element.text();
}else if(urlAddress.equals(Main.ESPN_URL)){
Elements element = doc.select("p");
article = element.text();
}
newsArticles.add(new Article(article, Main.SUMMARY_SENTENCES));
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("ERROR: No internet connection established.");
return;
}
}
public ArrayList<Article> getArticles() {
return newsArticles;
}
public Article getArticle(int i) {
if (newsArticles.size() <= i) {
return null;
} else {
return newsArticles.get(i);
}
}
//The method below does not recognize the "getSystemService" method, and when the method is no longer present there is a NetworkOnMainThreadException
private boolean isInternetAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
You need to execute web service connections asynchronous.
What I use in my projects is have a class ApiConnection and with interface get response. Example:
Apiconnection class
public class APIConnection extends AsyncTask<Object, String, Void> {
private final String TAG = "API-CONNECTION";
private StringBuilder sbuilder;
private JSONObject json;
private APIConnectionInterface mInterface;
protected int httpResponseCode = 0;
private String entity = null, url;
private APIConnectionType mmode;
private boolean DEBUG = BuildConfig.DEBUG;
private String[][] headers;
/**
Constructor For APIConnection
*/
public APIConnection(APIConnectionInterface thisdelegate, APIConnectionType mode, String murl, String entity) {
this.mInterface = thisdelegate;
this.mmode = mode;
this.url = murl;
this.entity = entity;
initHeaders();
}
private void initHeaders(){
headers = new String[][]{
{"token", "MY_TOKEN"},
{"Content-Type", "application/json;charset=utf-8"},
{"user-agent", "android"},
{"Accept-Language", "es"}
};
}
#Override
protected Void doInBackground(Object... params) {
BufferedReader buffer = null;
InputStreamReader in = null;
OutputStream os = null;
int timeoutConnection = 30000, timeoutSocket = 20000;
try{
sbuilder = new StringBuilder();
url = convertURL(url);
if (entity==null)entity="{}";
URL u = new URL(url);
HttpURLConnection conn;
if (url.startsWith("https://"))
conn = (HttpsURLConnection) u.openConnection();
else
conn = (HttpURLConnection) u.openConnection();
conn.setReadTimeout(timeoutConnection);
conn.setConnectTimeout(timeoutSocket);
for (String[] arr : headers){ conn.addRequestProperty(arr[0], arr[1]); }
/*GET*/if (mmode == APIConnectionType.GET) {
conn.setDoInput(true);
conn.setRequestMethod(mmode.toString());
httpResponseCode = conn.getResponseCode();
in = new InputStreamReader(
httpResponseCode == HttpURLConnection.HTTP_OK ? conn.getInputStream() : conn.getErrorStream(),"UTF-8");
/*OTHER*/} else if (mmode == APIConnectionType.POST || mmode == APIConnectionType.PUT ||
mmode == APIConnectionType.PATCH || mmode == APIConnectionType.DELETE) {
conn.setRequestMethod(mmode.toString());
conn.setDoOutput(true);
byte[] outputInBytes = entity.getBytes("UTF-8");
os = conn.getOutputStream();
os.write( outputInBytes );
httpResponseCode = conn.getResponseCode();
in = new InputStreamReader(
httpResponseCode == HttpURLConnection.HTTP_OK ? conn.getInputStream() : conn.getErrorStream(), "UTF-8");
}
if (in!=null){
buffer=new BufferedReader(in);
String line;
while ((line = buffer.readLine()) != null) {
sbuilder.append(line);
}
}else {
sbuilder.append("");
}
}
catch(IOException e) {
if (DEBUG)Log.d(TAG, "onBackground Exception " + e.getMessage());
sbuilder= new StringBuilder();
httpResponseCode = 0;
cancel(true);
return null;
} finally {
if (buffer != null) {
try {
buffer.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
if (os!=null){
try {
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
#Override
protected void onPostExecute(Void result){
try{
if (DEBUG) timelapse_e = System.currentTimeMillis();
if (sbuilder != null) {
json = new JSONObject(sbuilder.toString());
}
if (sbuilder != null){
sbuilder.setLength(0);
sbuilder.trimToSize();
}
sbuilder = null;
GoRunning();
hideDialog();
}
catch(RuntimeException e) {
if (DEBUG)Log.d(TAG, "PostExecute RuntimeException " + e.getMessage());
cancel(true);
}
catch(Exception e) {
if (DEBUG)Log.d(TAG, "PostExecute Exception " + e.getMessage());
cancel(true);
}
}
#Override protected void onCancelled() {
if (mInterface != null) mInterface.onCancelled(APIConnection.this);
super.onCancelled();
}
#Override protected void onPreExecute() {
super.onPreExecute();
if (DEBUG) timelapse_s = System.currentTimeMillis();
if (mInterface != null) mInterface.onStartLoading(APIConnection.this);
}
public void GoRunning(){
if (mInterface != null) try {
mInterface.onDataArrival(APIConnection.this, json, httpResponseCode);
} catch (JSONException e) {
onCancelled();
e.printStackTrace();
}
}
/**
* Hide Dialog (Progress dialog) if is showing and activity NOT Finishing
*/
private void hideDialog() {
if (mInterface != null) mInterface.onFinishedLoading(APIConnection.this);
}
/** <b>convertURL(String str);</b><br/>
* replaces any special characters to <b>%??</b><br/>
* Replacements actived:<br/>
* "{Space}" ==> "%20"
* #param str URL to encode
* #return url encoded
*/
public static String convertURL(String str) {
return str.trim().replace(" ", "%20");
// .replace("&", "%26")
// .replace(",", "%2c").replace("(", "%28").replace(")", "%29")
// .replace("!", "%21").replace("=", "%3D").replace("<", "%3C")
// .replace(">", "%3E").replace("#", "%23").replace("$", "%24")
// .replace("'", "%27").replace("*", "%2A").replace("-", "%2D")
// .replace(".", "%2E").replace("/", "%2F").replace(":", "%3A")
// .replace(";", "%3B").replace("?", "%3F").replace("#", "%40")
// .replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D")
// .replace("_", "%5F").replace("`", "%60").replace("{", "%7B")
// .replace("|", "%7C").replace("}", "%7D"));
}
public interface APIConnectionInterface {
void onDataArrival(APIConnection apiConnection, JSONObject json, int httpResponseCode) throws JSONException;
void onStartLoading(APIConnection apiConnection);
void onFinishedLoading(APIConnection apiConnection);
void onCancelled(APIConnection apiConnection);
}
public enum APIConnectionType {
GET("GET"),
POST("POST"),
PUT("PUT"),
PATCH("PATCH"),
DELETE("DELETE");
private String methodName;
APIConnectionType(String methodName){this.methodName = methodName;}
#Override public String toString() {return methodName;}
}
}
And then from any Activity or Fragment I can call the web service async
like this:
new APIConnection(new APIConnection.APIConnectionInterface() {
#Override public void onDataArrival(APIConnection apiConnection, JSONObject json, int httpResponseCode) {
try {
if (isHttpResponseOk(httpResponseCode, json)){//200 or 201
JSONObject obj = json.getJSONObject("results");
// do things with json
}
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override public void onStartLoading(APIConnection apiConnection) {showProgressDialog();}
#Override public void onFinishedLoading(APIConnection apiConnection) {hideProgressDialog();}
#Override public void onCancelled(APIConnection apiConnection) {hideProgressDialog();}
}, APIConnection.APIConnectionType.GET, MyApp.API_URL + "/master_data/", null).execute();
The only thing you need is to adapt the response to other object you need.
I hope that helps
I want to make my code wait until there is a change anywhere in my class to the variable finaloutcomes. Is there any way to do this? I am carrying this out within an Asynctask, which I posted below.
public HashMap<String,String> checkbetoutcome() {
new LoadAllGamet().execute();
// INSERT CODE HERE
return finaloutcomes;
}
ASYNCTASK
class LoadAllGamet extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
// HttpParams httpParameters = new BasicHttpParams();
// HttpConnectionParams.setConnectionTimeout(httpParameters, 250000);
//HttpConnectionParams.setSoTimeout(httpParameters, 250000);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url_check_bet);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param", bet));
// Log.d("CURRENTITEM", currentitem);
try {
post.setEntity(new UrlEncodedFormEntity(params));
} catch (IOException ioe) {
ioe.printStackTrace();
}
try {
HttpResponse response = client.execute(post);
Log.d("Http Post Responsecxxx:", response.toString());
HttpEntity httpEntity = response.getEntity();
InputStream is = httpEntity.getContent();
JSONObject jObj = null;
String json = "";
client.getConnectionManager().closeExpiredConnections();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("<", 0)) {
if (!line.startsWith("(", 0)) {
sb.append(line + "\n");
}
}
}
is.close();
json = sb.toString();
json = json.substring(json.indexOf('{'));
// Log.d("sbsssssssssss", json);
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
allgames = jObj.getJSONArray("bets");
// Log.d("WHAT IS MY ARRAY?", allgames.toString());
for (Integer i = 0; i < allgames.length(); i++) {
HashMap<String,String> statuses = new HashMap<>();
JSONObject c = allgames.getJSONObject(i);
JSONArray currentbet = c.getJSONArray("bet");
Log.d("Single array",currentbet.toString());
// Storing each json item in variable
for (Integer a = 0; a < currentbet.length();a++) {
JSONObject d = currentbet.getJSONObject(a);
String Result = d.getString("Result");
String id = d.getString("gid");
Log.d("RESULTS",Result);
statuses.put(id, Result);
}
allbetsmap.add(i, statuses);
Log.d("ddd", statuses.toString());
Log.d("AAA", allbetsmap.get(i).toString());
}
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
}
catch (IOException e) {
e.printStackTrace();
}
return "";
}
#Override
protected void onPostExecute(String param) {
Log.d("SIZE",Integer.toString(allbetsmap.size()));
//ArrayList<Map<String,String>> allbetsmap = new ArrayList<>();
//ArrayList<Map<String,String>> passtocheck = new ArrayList<>();
if (allbetsmap.size() == passtocheck.size()) {
for (int i = 0; i < allbetsmap.size();i++) {
if (allbetsmap.get(i).size() == passtocheck.get(i).size()) {
String finaloutcome = "won";
for (String a : allbetsmap.get(i).keySet()) {
String f = allbetsmap.get(i).get(a);
if(f.equals("null")) {
finaloutcome = "open";
}
else if (! (f.equals(passtocheck.get(i).get(a)))) {
finaloutcome = "lost";
break;
}
}
finaloutcomes.put(Integer.toString(i),finaloutcome);
}
}
}
Log.d("Vital",finaloutcomes.toString());
}
}
Ok, forget what I wrote before. I didn't realize you were writing code for android. Here is an improved version of LoadAllGamet. There are two important things here. 1. define as much as possible locally i.e. inside a method or - if that's not possible - inside the class. 2. return the result instead of putting it into some variable.
class LoadAllGamet extends AsyncTask<String, Void, HashMap<String,String>> {
protected HashMap<String,String> doInBackground(String ... args) {
HashMap<String,String> finaloutcomes = new HashMap<>(),
HashMap<Integer, HashMap<String,String>> allbetsmap = new HashMap<>();
HttpClient client = new DefaultHttpClient();
...
Log.d("SIZE",Integer.toString(allbetsmap.size()));
if (allbetsmap.size() == passtocheck.size()) {
...
}
Log.d("Vital",finaloutcomes.toString());
return finaloutcomes;
}
}
Whenever you want to do something that might take some time you should not run
that in the UI thread of you App since it can block your UI.
Instead run it asynchronously. One way of doing this is to use AsyncTask.
Let's assume you want to do something and while that something is being processed
you also want to update the UI (e.g. progress bars) from time to time. And once you
are finished you want to do something else with the result.
Here is one way of writing this.
void doSomething() {
new AsyncTask<String, Progress, Result>() {
protected Result doInBackground(String... args) {
//some code
publishProgress(values);
//some more code
return result;
}
protected void onProgressUpdate(Progress ... values) {
updateProgessBars(values);
}
protected void onPostExecute(Result result) {
doSomethingElse(result);
}
}.execute();
}
The String in new AsyncTask<String, Progress, Result> is the type of the
arguments to doInBackground. Often however you don't really need that unless
you want to pass arguments into execute.
Progress is the type of the values you want to send to onProgressUpdate. That
one you only need if you want to update your UI while the background processing
is still going on.
Result is of course your result type. Whatever you want to happen after
the doInBackground is finished you write into onPostExecute.
I have an asynctask that - when executed - is cancelled right away.
My class looks like this:
public class JSONParser extends AsyncTask<String, Void, JSONArray> {
private ListFragment fragment;
#Override
protected JSONArray doInBackground(String... strings) {
JSONArray ja = null;
String string = "";
try {
URL url = new URL(strings[0]);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
string = br.readLine();
ja = new JSONArray(string);
} catch (Exception e ) {
Log.w("Special", e.toString());
}
return ja;
}
public JSONParser(ListFragment fragment) {
this.fragment = fragment;
}
#Override
protected void onPostExecute(JSONArray jsonArray) {
try {
ArrayList<HashMap<String ,String>> datalist = new ArrayList<HashMap<String, String>>();
int i = 0;
while (i < jsonArray.length()) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject tool = jsonArray.getJSONObject(i);
map.put("id", tool.getInt("Id") + "");
map.put("name", tool.getString("Name"));
datalist.add(map);
i++;
}
fragment.setListAdapter(new SimpleAdapter(fragment.getActivity(), datalist, R.layout.tools_list, new String[] {"name"}, new int[] {R.id.text}));
} catch (Exception e) {
e.getMessage();
}
}
}
and from my fragment I'm calling it like this
AsyncTask task = new JSONParser(this).execute("http://10.0.2.2:1288/webservice/gettools.aspx");
Using the debugger I can see that as soon as the constructor is called, it skips to onCancelled() and returns. The URL is valid and working, I get no messages in the Log, and the JSON is valid.
Update: Also I have the required permission and OnCancelled() is called before it enters doInBackground(). doInBackground() is never called.
Any ideas?
I'm using IntelliJ and an AVD with Android 4.0.3.
Do you have set Internet permissions in your AndroidManifest?
Are you sure that in the doInBackground there is no exception fired?
Maybe it could be a better idea to also put the while (i < jsonArray.length()) part in the doInBackground :) (for performance)
Code to read String:
BufferedReader reader = new BufferedReader(yourInputStreamReader,8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
oehm...
you are creating your task with an activity object (this) (???)
AsyncTask task = new JSONParser(this).execute("http://10.0.2.2:1288/webservic /gettools.aspx");
but your task expects a View
public JSONParser(ListFragment fragment) {
im surprised that this doesnt throw an exception