我创建了一个android应用程序,我从json的服务器获取了一些数据。 当我使用wifi我的应用程序工作得很好,但当我使用边缘连接(移动互联网连接)我的应用程序停止
我的代码:
try {
JSONObject toSend = new JSONObject();
toSend.put("city_id", 6);
toSend.put("offset",con);
toSend.put("_count",10);
JSONObject json = toSend;
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000000);
JSONObject jsonResponse = null;
HttpPost post = new HttpPost("http://"+getResources().getString(R.string.address)+"/include/json/sale.php");
try {
StringEntity se = new StringEntity("json="+json.toString());
post.addHeader("content-type", "application/x-www-form-urlencoded");
post.setEntity(se);
HttpResponse response;
response = client.execute(post);
String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());
jsonResponse=new JSONObject(resFromServer);
} catch (Exception e) {
Log.e("ERROR", e.getMessage());
}
if(jsonResponse.get("products").toString().equals("{\"product\":\"empty\"}")){Log.d("Companies","NULL"); }
JSONArray array=jsonResponse.getJSONArray("products");
for (int i = 0; i < array.length(); i++) {
JSONObject json_data = array.getJSONObject(i);
JSONArray arr1=json_data.getJSONArray("product");
Pro_ID.add(arr1.getString(0));
Pro_Name.add(arr1.getString(1));
Log.d("get from host:", arr1.getString(1));
Log.d("get from ho****st:", arr1.getString(2));
Log.d("get from ho****st:", arr1.getString(6));
Pro_Logo.add("http://sellprojects.ir/"+arr1.getString(5));
Pro_Price.add(arr1.getString(3));
Pro_Descount.add(arr1.getString(4));
Pro_Company.add(arr1.getString(6));
}
con+=10;
} catch (JSONException e) {
Log.e("JSONException:::", e.getMessage());
}
Log Cat:
11-17 12:43:48.792:E / ERROR(21237):recvfrom失败:ECONNRESET(由对等方重置连接) 11-17 12:43:48.807:E / AndroidRuntime(21237):致命异常:AsyncTask#1 11-17 12:43:48.807:E / AndroidRuntime(21237):java.lang.RuntimeException:执行doInBackground()时发生错误 11-17 12:43:48.807:E / AndroidRuntime(21237):在android.os.AsyncTask $ 3.done(AsyncTask.java:299) 11-17 12:43:48.807:E / AndroidRuntime(21237):at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 11-17 12:43:48.807:E / AndroidRuntime(21237):at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 11-17 12:43:48.807:E / AndroidRuntime(21237):at java.util.concurrent.FutureTask.run(FutureTask.java:239) 11-17 12:43:48.807:E / AndroidRuntime(21237):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 11-17 12:43:48.807:E / AndroidRuntime(21237):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 11-17 12:43:48.807:E / AndroidRuntime(21237):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 11-17 12:43:48.807:E / AndroidRuntime(21237):at java.lang.Thread.run(Thread.java:838) 11-17 12:43:48.807:E / AndroidRuntime(21237):引起:java.lang.NullPointerException 11-17 12:43:48.807:E / AndroidRuntime(21237):at Fragments.SalesFragment $ MyAsyncTask.doInBackground(SalesFragment.java:186) 11-17 12:43:48.807:E / AndroidRuntime(21237):at Fragments.SalesFragment $ MyAsyncTask.doInBackground(SalesFragment.java:1) 11-17 12:43:48.807:E / AndroidRuntime(21237):在android.os.AsyncTask $ 2.call(AsyncTask.java:287) 11-17 12:43:48.807:E / AndroidRuntime(21237):at java.util.concurrent.FutureTask.run(FutureTask.java:234) 11-17 12:43:48.807:E / AndroidRuntime(21237):......还有4个 11-17 12:43:49.152:E / WindowManager(21237):活动com.example.rastak.MainActivity泄露了窗口com.android.internal.policy.impl.PhoneWindow $ DecorView {42a2e1b0 VE .... R .. .... D 0,0-513,144}最初是在这里添加的 11-17 12:43:49.152:E / WindowManager(21237):android.view.WindowLeaked:Activity com.example.rastak.MainActivity泄露了窗口com.android.internal.policy.impl.PhoneWindow $ DecorView {42a2e1b0 VE。 ...... R ...... D 0,0-513,144}最初是在这里添加的 11-17 12:43:49.152:E / WindowManager(21237):在android.view.ViewRootImpl。(ViewRootImpl.java:409) 11-17 12:43:49.152:E / WindowManager(21237):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:218) 11-17 12:43:49.152:E / WindowManager(21237):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 11-17 12:43:49.152:E / WindowManager(21237):在android.app.Dialog.show(Dialog.java:281) 11-17 12:43:49.152:E / WindowManager(21237):at Fragments.SalesFragment $ MyAsyncTask.onPreExecute(SalesFragment.java:245) 11-17 12:43:49.152:E / WindowManager(21237):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 11-17 12:43:49.152:E / WindowManager(21237):在android.os.AsyncTask.execute(AsyncTask.java:534) 11-17 12:43:49.152:E / WindowManager(21237):at Fragments.SalesFragment.onCreateView(SalesFragment.java:79) 11-17 12:43:49.152:E / WindowManager(21237):在android.app.Fragment.performCreateView(Fragment.java:1695) 11-17 12:43:49.152:E / WindowManager(21237):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885) 11-17 12:43:49.152:E / WindowManager(21237):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 11-17 12:43:49.152:E / WindowManager(21237):在android.app.BackStackRecord.run(BackStackRecord.java:694) 11-17 12:43:49.152:E / WindowManager(21237):在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) 11-17 12:43:49.152:E / WindowManager(21237):在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:441) 11-17 12:43:49.152:E / WindowManager(21237):在android.os.Handler.handleCallback(Handler.java:725) 11-17 12:43:49.152:E / WindowManager(21237):在android.os.Handler.dispatchMessage(Handler.java:92) 11-17 12:43:49.152:E / WindowManager(21237):在android.os.Looper.loop(Looper.java:153) 11-17 12:43:49.152:E / WindowManager(21237):在android.app.ActivityThread.main(ActivityThread.java:5341) 11-17 12:43:49.152:E / WindowManager(21237):at java.lang.reflect.Method.invokeNative(Native Method) 11-17 12:43:49.152:E / WindowManager(21237):at java.lang.reflect.Method.invoke(Method.java:511) 11-17 12:43:49.152:E / WindowManager(21237):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:929) 11-17 12:43:49.152:E / WindowManager(21237):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 11-17 12:43:49.152:E / WindowManager(21237):at dalvik.system.NativeStart.main(Native Method)