Im trying to create an app that gets a json object from a url.
This is proving to be unnecessarily frustrating as it keeps crashing on the activity that is supposed to load and parse the json object. It just pops up the message "Unfortunately, (AppName) has stopped." and then exits the application. The data from the JSON is never shown on the screen. Here is the code with the activity and the JSON parsing
package com.example.Accomplist;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RemoteException;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
/**
* Created with IntelliJ IDEA.
* User: DESAI_628IL
* Date: 3/1/13
* Time: 7:34 PM
* To change this template use File | Settings | File Templates.
*/
public class MainScreen extends Activity{
TextView myTextView;
// HttpClient client;
// url to make request
final static String url = "http://accomplist.herokuapp.com/api/v1/sharedevent/2/?format=json";
private static final String TAG_EVENT="event"; //A JSON object within the JSON object that will be returned by JSONParse()
private static final String TAG_DESCRIPTION="description"; //A JSON tag within the JSON object EVENT
private static String eventString="Yo";
static JSONObject json;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
new JSONParse().execute(url);
}
private class JSONParse extends AsyncTask<String, String, String> {
HttpClient client;
JSONObject jsonObj= null;
#Override
protected String doInBackground(String... jsonurl) {
StringBuilder url= new StringBuilder(String.valueOf(jsonurl));
HttpGet get= new HttpGet(url.toString());
HttpResponse r= null;
try {
r = client.execute(get);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
int status= r.getStatusLine().getStatusCode();
if (status==200){
HttpEntity e=r.getEntity();
String data= null;
try {
data = EntityUtils.toString(e);
} catch (IOException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
jsonObj = new JSONObject(data);
} catch (JSONException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
JSONObject eventJson= jsonObj.getJSONObject(TAG_EVENT);
eventString= eventJson.getString(TAG_DESCRIPTION);
}
catch (JSONException e1) {
eventString="Couldn't Parse Data";
}
return eventString;
}
else{
return eventString;
}
}
protected void onProgressUpdate(String... progress) {
Toast loadingToast= Toast.makeText(getApplicationContext(), "Loading", Toast.LENGTH_LONG);
loadingToast.show();
}
protected void onPostExecute(String result) {
eventString=result;
myTextView = (TextView)findViewById(R.id.textView1);
myTextView.setText(eventString);
}
}
}
I hope someone can help. Ive been stuck on this for the longest time. Ive tried many things. Ive tried different ways of getting the JSON object from the url, putting the parsing code in a different class, and lots of other small things. Any help would be appreciated. Here is the LogCat error report
02-26 12:18:46.691: ERROR/ThrottleService(324): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)
02-26 12:19:53.601: WARN/dalvikvm(4073): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:19:55.121: WARN/dalvikvm(4073): threadid=12: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:19:57.632: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4073 uid 10048
02-26 12:20:03.452: WARN/dalvikvm(4094): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:20:04.744: ERROR/AndroidRuntime(4094): FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 0: [Ljava.lang.String;#40d12070
at java.net.URI.create(URI.java:727)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:84)
at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:78)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
... 4 more
02-26 12:20:10.512: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4094 uid 10048
02-26 12:20:24.401: WARN/dalvikvm(4111): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:20:24.441: ERROR/AndroidRuntime(4111): FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 0: [Ljava.lang.String;#40d12038
at java.net.URI.create(URI.java:727)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:84)
at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:78)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
... 4 more
02-26 12:20:29.411: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4111 uid 10048
try something like this..
HttpClient client = new DefaultHttpClient();
// Perform a GET request for a JSON list
HttpUriRequest request = new HttpGet("https://somejson.json");
// Get the response that sends back
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Try with this
new JSONParse().execute("");
instead of
new JSONParse().execute(url);
can u paste your logcat message so that i can help u more accurately.
The parameter in the doInbackgroud() is work like an array so that you can pass multiple argument to this method while calling the execute method on AsyncTask. The first passed parameter would be stored on the 0th position and so on. Since you are only passing one parameter to the execute method, that parameter would be stored on the 0th position... So you need to get the URL from jsonurl[0]
Try with
StringBuilder url= new StringBuilder(String.valueOf(jsonurl[0]));
Change your doInBackground method as :
#Override
protected String doInBackground(String... jsonurl) {
StringBuilder url= new StringBuilder(String.valueOf(jsonurl[0]));
HttpGet get= new HttpGet(url.toString());
HttpResponse r= null;
currently you are passing wrong url to get data form web service. see following to known move how we get value from varargs inside doInBackground method
What does the "..." mean in a parameter list? doInBackground(String... params)
You Haven't get your Http client
set
HttpClient httpClient = new DefaultHttpClient(); in your doInBackGround()
You have 2 mistakes
1. StringBuilder url= new StringBuilder(String.valueOf(jsonurl[0]));
// jsonurl[0]
2. client = new DefaultHttpClient();
// Add this line too
Related
In my android app I've used a async task to handle my internet request. I achieved that by using the okhttp library, which makes it easy to make internet requests. I have a lot of the code surrounded my try/catch statements, but they somehow do not trigger when there is an unhandled exception so the app crashes. That only happens when the internet is disconnected or is slow in areas.
I've looked for similar posts, but found out that people who had the same problems didn't try/catch, so the exceptions weren't handled
I've also tried to make a checker for the internet accessibility, but that didn't work out too well, because i would much rather do it with clean try-catching.
private class prenesi extends AsyncTask<String, Void, String>
{
String odgovor = null;
protected String doInBackground(String... params)
{
OkHttpClient client = new OkHttpClient();
RequestBody vsebina = new FormEncodingBuilder()
.add("kraj", params[0])
.build();
Request request = new Request.Builder()
.url("http://mypage.com/login.php")
.post(vsebina)
.build();
Response response = null;
try
{
response = client.newCall(request).execute();
}
catch (IOException e)
{
e.printStackTrace();
}
if (!response.isSuccessful())
{
odgovor = "Napaka.";
}
else
try
{
odgovor = response.body().string();
}
catch (IOException e)
{
e.printStackTrace();
}
return odgovor;
}
}
The devconsole says the problem is in the doInBackground() and i guess it caused because the null pointer exception, though I'm not entirely sure what that means:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
Could somebody point me towards the flaw please. I'm stuck with this.
All the help will be very appreciated, hope to solve this problem soon.
Thanks for contributing.
It seems that you forgot to initialize your String value. Run it by first setting String odgovor=""; instead of String odgovor= null;
Try modifying the code to this
String odgovor="";
try{
response = client.newCall(request).execute();
if (!response.isSuccessful()) {
odgovor = "Napaka.";
} else{
odgovor = response.body().string();
}} catch (IOException e) {
e.printStackTrace();
}
Seems the problem in the end was with the isSuccessul method, which was a bit cheesy, idk why, but instead of it i used if(response == null).
How do you handle the RuntimeException when AsyncTask couldn't connect to server since server is "down" for some reason?
What do I try is a bunch of catch blocks (doesn't help):
try {
// Create Request to server and get response
HttpGet httpget = new HttpGet(activity.getString(R.string.get_channels_lang_host));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
downloadedString = httpclient.execute(httpget, responseHandler);
}catch (MalformedURLException e) {
Log.e("URL:","is a malformed URL");
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("URL:"," UnsupportedEncodingException");
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.e("URL:"," ClientProtocolException");
} catch (SocketTimeoutException e) {
e.printStackTrace();
Log.e("URL:"," SocketTimeoutException");
} catch (ConnectTimeoutException e) {
e.printStackTrace();
Log.e("URL:"," ConnectTimeoutException");
} catch (IOException e) {
Log.e("URL:","IOException");
e.printStackTrace();
}
if (downloadedString != null) {
//parse request to object
dataChannelsLangArrayList = JsonParser.getDataChannelsLang(downloadedString);
} else {
Toast.makeText(ActivityLoading.this, "Failed to load data. Restarting...", Toast.LENGTH_LONG).show();
Global.restartApp(ActivityLoading.this);
}
i'm still getting the runtime error:
04-09 11:31:29.610 3696-3719/tenkol.design.com.imbrecords E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.widget.Toast$TN.<init>(Toast.java:322)
at android.widget.Toast.<init>(Toast.java:91)
at android.widget.Toast.makeText(Toast.java:238)
at tenkol.design.com.imbrecords.Global.restartApp(Global.java:115)
at tenkol.design.com.imbrecords.ActivityLoading$LoadingAsyncTask.getChannelsLang(ActivityLoading.java:249)
at tenkol.design.com.imbrecords.ActivityLoading$LoadingAsyncTask.doInBackground(ActivityLoading.java:157)
at tenkol.design.com.imbrecords.ActivityLoading$LoadingAsyncTask.doInBackground(ActivityLoading.java:141)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:864)
I'm mostly interested in two cases:
When server doesn't respond at all;
When the time spend for request is too big (i.e. with slow Internet).
What is the proper way to handle those?
Can't create handler inside thread that has not called Looper.prepare()
Well, you have created a Handler, but the thread it is linked to doesn't have any message queue to post stuff on.
Try new Handler(Looper.getMainLooper()) for a handler linked to main thread.
Then you can post a Runnable on it which shows a toast.
But seriously, why not collect exception and result both and handle it all in onPostExecute() ?
Public class Result<T>{
public T data;
public Exception exception;
public String message;
}
I recently uploaded an update of my app to Google Play. When trying it out, I get an error when clicking on a manually added banner (e.g. not AdMob or anything like that). The click is setting of an AsyncTask that sends data to a php-script using POST. I use ProGuard so when I send the error report to myself I am only able to see the method in which the error is caused: doInBackground. I am also able to see that it is a NullPointerException. The strange thing is, when I run the app on my device from my computer using Eclipse, the error doesn't occur, so I can't really find what line is causing the NullPointerException. It has worked perfectly in previous versions of my app, and I haven't made any changes to this code. And when I look through the method doInBackground, I can't find any place where a NullPointerException might occur.
Here is the error message from my developers console, this is proguarded though:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at se.nollekollen.main.d.a(Unknown Source)
at se.nollekollen.main.d.doInBackground(Unknown Source)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
And here is my doInBackground:
#Override
protected String doInBackground(String... arg0) {
String url_stats = "http://xxxxx.xx/statistik/sendStatistics_click.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("activity", getClass().getEnclosingClass().getName().substring(20)));
params.add(new BasicNameValuePair("section", SECTION));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_stats);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
The only thing that could be null here is actually "SECTION", but that wouldn't cause a NullPointerException since the value can be null when creating a new BasicNameValuePair(key, value).
Can anyone find what could cause the NullPointerException? And how is it possible that this ONLY occurs when the app is downloaded from Google Play, and not when run from the computer?
Original Question -
I was following a tutorial on json in android, but having problem to get json value using Async in android. First I created jsonparser class and added following to it -
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
// Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
// Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
Then to use this in fragment by async i tried -
class getTopicId extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Getting Topic of the Day ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl("http://medicalguru.in/android/tod.php");
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
topic_id = json.getInt("value");
}
catch (JSONException e) {
e.printStackTrace();
}
finally {
Toast.makeText(getActivity(), "topic id -"+topic_id, Toast.LENGTH_LONG).show();
}
}
}
but the app is crashing when i execute this async.
my logcat shows -
03-06 15:07:03.123: E/AndroidRuntime(2041): FATAL EXCEPTION: main
03-06 15:07:03.123: E/AndroidRuntime(2041): java.lang.NullPointerException
03-06 15:07:03.123: E/AndroidRuntime(2041): at in.medicalguru.DailyTestFragment$getTopicId.onPostExecute(DailyTestFragment.java:786)
03-06 15:07:03.123: E/AndroidRuntime(2041): at in.medicalguru.DailyTestFragment$getTopicId.onPostExecute(DailyTestFragment.java:1)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.os.Looper.loop(Looper.java:137)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-06 15:07:03.123: E/AndroidRuntime(2041): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 15:07:03.123: E/AndroidRuntime(2041): at java.lang.reflect.Method.invoke(Method.java:525)
03-06 15:07:03.123: E/AndroidRuntime(2041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-06 15:07:03.123: E/AndroidRuntime(2041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-06 15:07:03.123: E/AndroidRuntime(2041): at dalvik.system.NativeStart.main(Native Method)
The link mentioned above in the code outputs as -
{"value":1}
when opened normally, but when I run in async it says err related to nullpointer?
Couldn't figure, where I am making mistake ?
Edit 1 :
*tod.php* code (Since Matthew has pointed something which I dont understand, so it may be helpful)
<?php
$today = date("Ymd");
switch($today){
case 20140304 :
$tod["value"] = 24;
break;
case 20140305 :
$tod["value"] = 25;
break;
case 20140306 :
$tod["value"] = 1;
break;
default:
$tod["value"] = 1;
break;
}
echo json_encode($tod);
?>
Edit 2 : *Found the problem (fully for me) but Partly in Real* - A special thanks to Matthew for his idea, to uncomment the error logs of function getJSONFromUrl(String url). I got a new error line in logcat -
Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject
After little bit of search on google and stacksoverflow I found one suggestion to change chrSet to UTF-8 instead of iso-8859-1. and it worked for my requested url in both situations (with or without WWW - suggestion to test, by nikis). But this answer is partial real, because, now 2 unanswered questions developed -
1. Why did chrSet change worked? In future, what chrSet to be used, to avoid this problem, and what's the use of other one chrSet?
2. Matthew has replicated my Json to his server mwesly.com/test/ . Trial on his server, logcat shows following error in all cases (with or without WWW, with or without using index.php in the end) -
Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
Now, why is this error appearing on his server? What should be done to prevent/treat this error.
If I need to ask these updated questions separately, please let me know.
It looks like json is null on following line, which is giving you the NullPointerException.
topic_id = json.getInt("value");
I think this is because your parser is having issues parse the json. I wrote a quick python script and tried to parse the page and it failed as well. For some reason, the response I get from http://medicalguru.in/android/tod.php is:
\xef\xbb\xbf{"value":1}
I am not sure why the \xef\xbb\xbf bytes are there, but they are likely causing the problem.
edit: a quick google search says it is the UTF-8 BOM. If you remove it your android code should work.
edit2: I put an example json on http://mwesly.com/test. Try hitting that json to see if your android code is correct. If it works, it means there is a problem with your endpoint, not your android code.
edit3: example of using loopj:
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://mwesly.com/test", null, new JsonHttpResponseHandler() {
#Override
public void onSuccess(JSONObject json) {
// do something with the json
int value = json.getInt("value");
}
});
I'm going to head to bed, if you still haven't solved this problem when I wake up, I'll give it another look. Good luck and happy coding!
Try to use the following getJSONFromUrl method:
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
return new JSONObject(EntityUtils.toString(httpResponse.getEntity()));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private JSONObject jObj = null;
private JSONArray JArr = null;
and then after;
URL url = null;
try {
url = new URL("your url is here");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JSONParser jParser = new JSONParser();
String json_value = jParser.getJSONFromUrl(url);
try {
JArr = new JSONArray(json_value);
} catch (JSONException e) {
e.printStackTrace();
}
jObj = null;
for (int i = 0; i < JArr.length(); i++) {
try {
jObj = JArr.getJSONObject(i);
topic_id = jObj.getInt("value");
} catch (JSONException e) {
e.printStackTrace();
}
}
I am using the below code to fetch xml file from the server and due to heavy and large xml file, it got crashed and showing Out of memory Issue.
public class Connect {
static BufferedReader in=null;
String result=null;
Context context;
//Establish connection with web server
public String HTTPConnect(String uri1,List<NameValuePair> list,Context context)
{
this.context=context;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri1);
if(list!=null)
{
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
httpPost.setEntity(formEntity);
}
HttpResponse httpResponse = httpClient.execute(httpPost);
in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
long heapSize = Runtime.getRuntime().totalMemory();
if(in!=null)
{
while ((line = in.readLine()) != null) {//crasheg here
sb.append(line + NL);
}
in.close();
}
result = sb.toString();
}
catch(UnsupportedEncodingException e)
{
String err = (e.getMessage()==null)?"Cant connect to server":e.getMessage();
ShowDialog();
}
catch (MalformedURLException e) {
String err = (e.getMessage()==null)?"Malformed Exception":e.getMessage();
ShowDialog();
}
catch(Exception ex)
{
String err = (ex.getMessage()==null)?"NetworkConnectionException":ex.getMessage();
ShowDialog();
}
finally {
if (in != null) {
try {
in.close();
} catch (Exception ex) {
String err = (ex.getMessage()==null)?"Excepion":ex.getMessage();
ex.printStackTrace();
}
}
}
return result;
}
I know that that I am copying the whole XML in String and due to heavy file, It gor crashing. It is working fine in small size of XML file, but what are the alternative for large xml file. I am uing SAX parser for parsing this xml file.
[EDIT]
Below is the logcat:
FATAL EXCEPTION: AsyncTask #4
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)
Caused by: java.lang.OutOfMemoryError
java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:97)
java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:136)
java.lang.StringBuilder.append(StringBuilder.java:272)
java.io.BufferedReader.readLine(BufferedReader.java:452)
com.kxs.appitize.Connect.HTTPConnect(Connect.java:56)
com.kxs.appitize.ListRestaurants$Asyn_rest.doInBackground(ListRestaurants.java:168)
com.kxs.appitize.ListRestaurants$Asyn_rest.doInBackground(ListRestaurants.java:1)
01-at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
Activity com.kxs.appitize.TabsMainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#44f4fb28 that was originally added here
android.view.WindowLeaked: Activity com.kxs.appitize.TabsMainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#44f4fb28 that was originally added here
at android.view.ViewRoot.<init>(ViewRoot.java:247)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at com.kxs.appitize.ListRestaurants$Asyn_rest.onPreExecute(ListRestaurants.java:156)
at android.os.AsyncTask.execute(AsyncTask.java:391)
at com.kxs.appitize.ListRestaurants.onCreate(ListRestaurants.java:133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
at com.kxs.appitize.TabGroupActivity.startChildActivity(TabGroupActivity.java:72)
at com.kxs.appitize.ListCategories$1.onClick(ListCategories.java:109)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
copying the whole XML in String and due to heavy file
Bad idea. First you need to cache the xml in a temporary directory. It should never be in the RAM.
Once you do that use SAX to parse the file. During parsing try not to keep the whole structure in the RAM, instead parse in bite sized chunks.
Try to use inputStream to parse XML. The inputStrem does not increase Android stack size and it's prevent possibles OOM with large String. I have the same things with large JSON and now, i use Jackson to parse JSON directly from Stream.
You can find equivalent library for parsing XML.