I'm stuck for hours with an Exception from HttpRequest method and I'm really tired of this. Did a google research, but i can't find any answer to fix it. I need to do a http request to get data from database via PHP scripts. Everything is going well, when I call method for all lines from table with params:
url - "http://android-connection.cba.pl/get_all_notes.php" method - "GET" List - empty list
But it spits out an Exception when i trying to get specify id line with parameters:
url - "http://android-connection.cba.pl/get_note.php" method - "GET" List - [noteid=1]
I call it from doInBackground method in class, which extends AsyncTask. There is this problematic method, if you could help me with this.. thanks!
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
Log.e("URL", url);
HttpResponse httpResponse = httpClient.execute(httpGet);
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();
Log.e("JSON", json);
} 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;
}
and there are logs:
05-06 08:50:35.647: D/AndroidRuntime(3119): Shutting down VM
05-06 08:50:35.647: W/dalvikvm(3119): threadid=1: thread exiting with uncaught exception (group=0xb3ab9ba8)
05-06 08:50:35.697: E/AndroidRuntime(3119): FATAL EXCEPTION: main
05-06 08:50:35.697: E/AndroidRuntime(3119): Process: com.example.notatki, PID: 3119
05-06 08:50:35.697: E/AndroidRuntime(3119): android.os.NetworkOnMainThreadException
05-06 08:50:35.697: E/AndroidRuntime(3119): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
05-06 08:50:35.697: E/AndroidRuntime(3119): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-06 08:50:35.697: E/AndroidRuntime(3119): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-06 08:50:35.697: E/AndroidRuntime(3119): at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-06 08:50:35.697: E/AndroidRuntime(3119): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-06 08:50:35.697: E/AndroidRuntime(3119): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-06 08:50:35.697: E/AndroidRuntime(3119): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-06 08:50:35.697: E/AndroidRuntime(3119): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-06 08:50:35.697: E/AndroidRuntime(3119): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-06 08:50:35.697: E/AndroidRuntime(3119): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-06 08:50:35.697: E/AndroidRuntime(3119): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-06 08:50:35.697: E/AndroidRuntime(3119): at com.example.notatki.JSONParser.makeHttpRequest2(JSONParser.java:64)
05-06 08:50:35.697: E/AndroidRuntime(3119): at com.example.notatki.EditNoteActivity$GetNoteDetails$1.run(EditNoteActivity.java:145)
05-06 08:50:35.697: E/AndroidRuntime(3119): at android.os.Handler.handleCallback(Handler.java:733)
05-06 08:50:35.697: E/AndroidRuntime(3119): at android.os.Handler.dispatchMessage(Handler.java:95)
05-06 08:50:35.697: E/AndroidRuntime(3119): at android.os.Looper.loop(Looper.java:136)
05-06 08:50:35.697: E/AndroidRuntime(3119): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-06 08:50:35.697: E/AndroidRuntime(3119): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 08:50:35.697: E/AndroidRuntime(3119): at java.lang.reflect.Method.invoke(Method.java:515)
05-06 08:50:35.697: E/AndroidRuntime(3119): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-06 08:50:35.697: E/AndroidRuntime(3119): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-06 08:50:35.697: E/AndroidRuntime(3119): at dalvik.system.NativeStart.main(Native Method)
It sounds like you are either not writing or not using your AsyncTask
correctly. This is my version that should do what you are attempting. I follow up with how to call this. There are a few common mistakes, such as directly calling the doInBackground
method instead of execute
, so this should clear anything up.
To wrap this code in an AsyncTask
, create the following class
:
public class MyJSONRequest extends AsyncTask<Void, Void, Void> {
String url;
String method;
List<NameValuePair> params;
public MyJSONRequest(String url, String method, List<NameValuePair> params) {
this.url = url;
this.method = method;
this.params = params;
}
@Override
public Void doInBackground(Void...) {
makeHttpRequest(url, method, params)
}
}
Now, to complete the task with no errors, do:
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("noteid", "1"));
new MyJSONRequest("http://android-connection.cba.pl/get_note.php", "GET", list).execute();
Also, you should note that if(method == "POST")
and if(method == "POST")
will not resolve how you are expecting. You need to change these to if(method.equals("POST"))
and if(method.equals("GET"))
.