![](/img/trans.png)
[英]HttpResponse using android issue: execute always causes exception?
[英]Exception in HttpResponse execute in android
我被HttpRequest方法的Exception困擾了幾個小時,我真的對此感到厭倦。 谷歌做了研究,但我找不到任何答案來解決它。 我需要執行一個http請求,以通過PHP腳本從數據庫獲取數據。 當我使用params調用表中所有行的方法時,一切進展順利:
url-“ http://android-connection.cba.pl/get_all_notes.php ”方法-“ GET”列表-空列表
但是當我嘗試獲取帶有參數的指定id行時,它會拋出一個異常:
url-“ http://android-connection.cba.pl/get_note.php ”方法-“ GET”列表-[noteid = 1]
我從類的doInBackground方法調用它,該方法擴展了AsyncTask。 如果您可以幫助我,這就是有問題的方法..謝謝!
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;
}
並且有日志:
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)
聽起來您要么沒有編寫,要么沒有正確使用AsyncTask
。 這是我的版本,應該可以執行您的嘗試。 我跟進如何稱呼它。 有一些常見的錯誤,例如直接調用doInBackground
方法而不是execute
,因此應該清除所有內容。
將此代碼包裝在AsyncTask
,請創建以下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)
}
}
現在,要完成任務且沒有錯誤,請執行以下操作:
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();
另外,您應該注意if(method == "POST")
和if(method == "POST")
不會解決您的期望。 您需要將它們更改為if(method.equals("POST"))
和if(method.equals("GET"))
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.