httpclient.execute(httppost)在AsyncTask中崩溃

时间:2015-10-21 20:09:07

标签: java android android-asynctask http-post androidhttpclient

我曾尝试为我的Android应用程序创建一个Server Request类,它在httpclient.execute(httppost)崩溃了。即使它在try / catch内部,应用程序也会崩溃,而不是进入catch部分。

代码:

public static JSONObject ConvertHttpResponse(HttpResponse response) throws ParseException, IOException, JSONException {
    //In: A httpResponse
    //Out: A converted jsonObject from the httpResponse
    HttpEntity entity=response.getEntity();
    String result=EntityUtils.toString(entity);
    JSONObject jObject = new JSONObject(result);
    if(jObject.length()==0)
        return null;
    return jObject;
}
  public class DataAsyncSend extends AsyncTask<Void,Void,JSONObject> {
        GetCallback callback;

        public DataAsyncSend(GetCallback callback) {
            this.callback=callback;
        }

        @Override
        protected JSONObject doInBackground(Void... params) {
            HttpParams httpRequestParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpRequestParams, SERVER_CONNECTION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(httpRequestParams, SERVER_CONNECTION_TIMEOUT);
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(SERVER_ADRESS + pageToSend);
            try {
                post.setEntity(new UrlEncodedFormEntity(data));
                HttpResponse resposnse= client.execute(post); //crashes here
                return ConvertHttpResponse(resposnse);

            } catch (Exception e) {
               e.printStackTrace();
                return null;
            }

        }
    @Override
    protected void onPostExecute(JSONObject response) {
        progressDialog.dismiss();
        callback.done(response);
        super.onPostExecute(response);
    }

日志:

10-21 21:16:45.284: W/EGL_genymotion(2711): eglSurfaceAttrib not implemented
10-21 21:16:45.836: W/System.err(2711): org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
10-21 21:16:45.836: W/System.err(2711):     at org.json.JSON.typeMismatch(JSON.java:111)
10-21 21:16:45.836: W/System.err(2711):     at org.json.JSONObject.<init>(JSONObject.java:158)
10-21 21:16:45.836: W/System.err(2711):     at org.json.JSONObject.<init>(JSONObject.java:171)
10-21 21:16:45.836: W/System.err(2711):     at Networking.ServerRequests.ConvertHttpResponse(ServerRequests.java:95)
10-21 21:16:45.836: W/System.err(2711):     at Networking.ServerRequests$DataAsyncSend.doInBackground(ServerRequests.java:122)
10-21 21:16:45.836: W/System.err(2711):     at Networking.ServerRequests$DataAsyncSend.doInBackground(ServerRequests.java:1)
10-21 21:16:45.836: W/System.err(2711):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-21 21:16:45.836: W/System.err(2711):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-21 21:16:45.836: W/System.err(2711):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-21 21:16:45.836: W/System.err(2711):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-21 21:16:45.836: W/System.err(2711):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-21 21:16:45.836: W/System.err(2711):     at java.lang.Thread.run(Thread.java:856)
10-21 21:16:45.860: D/AndroidRuntime(2711): Shutting down VM
10-21 21:16:45.860: W/dalvikvm(2711): threadid=1: thread exiting with uncaught exception (group=0xa613e908)
10-21 21:16:45.860: E/AndroidRuntime(2711): FATAL EXCEPTION: main
10-21 21:16:45.860: E/AndroidRuntime(2711): java.lang.NullPointerException
10-21 21:16:45.860: E/AndroidRuntime(2711):     at com.example.fififun.User.<init>(User.java:22)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at com.example.fififun.login$2.done(login.java:114)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at Networking.ServerRequests$DataAsyncSend.onPostExecute(ServerRequests.java:133)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at Networking.ServerRequests$DataAsyncSend.onPostExecute(ServerRequests.java:1)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at android.os.Looper.loop(Looper.java:137)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at java.lang.reflect.Method.invokeNative(Native Method)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at java.lang.reflect.Method.invoke(Method.java:511)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-21 21:16:45.860: E/AndroidRuntime(2711):     at dalvik.system.NativeStart.main(Native Method)

编辑: 错误是响应为空,无法将httpRespone解析为JSONObject。谢谢你的帮助!

0 个答案:

没有答案