带有JSON解析器的Android httpresponse

时间:2014-11-30 13:37:10

标签: android json

我的应用程序在我的JSON解析器类中一直崩溃。 问题是我正在遵循一个教程,当它是localhost上的mysql时工作正常。 现在我在我的hostgator帐户上托管它。由于代码是很多类,我放入了很多日志标识符,以帮助我缩小预测的确切位置。从logcat我无法弄清楚错误是什么。

代码(JSON解析器)

    package databasehandler;

/**
 * Created by YP on 26-Nov-14.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;


public class JSONParser
{

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
        Log.d("Chk","JSON parser function entered in JSON parser");

        // Making HTTP request
        try
        {
            Log.d("Chk","JSON parser try method entered in JSON parser");
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            Log.d("Chk","create new http client in JSON parser");
            HttpPost httpPost = new HttpPost(url);
            Log.d("Chk","new httppost url in JSON parser");
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            Log.d("Chk","new url encoded form entity in JSON parser");

            Log.d("Chk","new http response in JSON parser");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            Log.d("Chk","new http response in JSON parser");
            HttpEntity httpEntity = httpResponse.getEntity();
            Log.d("Chk","new http entity in JSON parser");
            is = httpEntity.getContent();
            Log.d("Chk","end of try in JSON parser");

        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
            Log.d("Chk","caught unsupported exception in JSON parser");
        } catch (ClientProtocolException e)
        {
            e.printStackTrace();
            Log.d("Chk","caught client protocol exception in JSON parser");
        } catch (IOException e)
        {
            e.printStackTrace();
            Log.d("Chk","caught IO exception in JSON parser");

        }

        try
        {
            Log.d("Chk","entered second try method in JSON parser");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            Log.d("Chk","entered second try method and before while in JSON parser");
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            Log.d("Chk","entered second try method after while in JSON parser");
            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;

    }
}

logcat的

11-30 13:15:06.548: D/Chk(2109): Login button event enetered
11-30 13:15:06.548: D/Chk(2109): JSON parser function entered in JSON parser
11-30 13:15:06.548: D/Chk(2109): JSON parser try method entered in JSON parser
11-30 13:15:06.548: D/Chk(2109): create new http client in JSON parser
11-30 13:15:06.548: D/Chk(2109): new httppost url in JSON parser
11-30 13:15:06.548: D/Chk(2109): new url encoded form entity in JSON parser
11-30 13:15:06.548: D/Chk(2109): new http response in JSON parser
11-30 13:15:06.551: D/AndroidRuntime(2109): Shutting down VM
11-30 13:15:06.554: W/AudioTrack(1218): AUDIO_OUTPUT_FLAG_FAST denied by client
11-30 13:15:06.562: E/AndroidRuntime(2109): FATAL EXCEPTION: main
11-30 13:15:06.562: E/AndroidRuntime(2109): Process: com.techiequickie.bharath.boadraf, PID: 2109
11-30 13:15:06.562: E/AndroidRuntime(2109): android.os.NetworkOnMainThreadException
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at java.net.InetAddress.getAllByName(InetAddress.java:215)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at databasehandler.JSONParser.getJSONFromUrl(JSONParser.java:55)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at databasehandler.UserFunctions.loginUser(UserFunctions.java:42)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at com.techiequickie.bharath.boadraf.Loginactivity$1.onClick(Loginactivity.java:93)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.view.View.performClick(View.java:4756)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.view.View$PerformClick.run(View.java:19749)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.os.Handler.handleCallback(Handler.java:739)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.os.Looper.loop(Looper.java:135)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.app.ActivityThread.main(ActivityThread.java:5221)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at java.lang.reflect.Method.invoke(Native Method)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at java.lang.reflect.Method.invoke(Method.java:372)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-30 13:15:06.568: W/ActivityManager(1218):   Force finishing activity com.techiequickie.bharath.boadraf/.Loginactivity
11-30 13:15:07.083: W/ActivityManager(1218): Activity pause timeout for ActivityRecord{3729739a u0 com.techiequickie.bharath.boadraf/.Loginactivity t79 f}
11-30 13:15:08.158: W/AudioTrack(1218): AUDIO_OUTPUT_FLAG_FAST denied by client
11-30 13:15:08.338: I/Process(2109): Sending signal. PID: 2109 SIG: 9
11-30 13:15:08.340: W/InputDispatcher(1218): channel '384fe924 com.techiequickie.bharath.boadraf/com.techiequickie.bharath.boadraf.WelcomeLogo (server)' ~ Consumer closed input channel or an error occurred.  events=0x9

1 个答案:

答案 0 :(得分:0)

问题是您正在应用程序的主线程上访问网络功能(发出HTTP请求)。这在Android中并不是一个好习惯,在较新版本的Android中,它会引发此异常。修复方法是将您的网络操作(以及任何其他长时间运行或阻塞操作)移动到另一个线程。您可以使用自己的工作线程,线程池甚至AsyncTask来完成此任务。