Android 总是意外地停止

Android 总是意外地停止,android,json,Android,Json,我在谷歌上搜索了很多关于从url获取json字符串的信息,但我认为是时候问这个问题了。这个问题的几乎所有算法都不适合我。它真的需要与AsyncTask一起使用吗?我是android的初学者,所以我知道的不多。请帮帮我。或者如果你能提供一个更好的算法,请这样做 像这样使用BufferedReader对象读取 public JSONObject getJSONFromUrl(String url) { InputStream is = null; JSONObject jOb

我在谷歌上搜索了很多关于从url获取json字符串的信息,但我认为是时候问这个问题了。这个问题的几乎所有算法都不适合我。它真的需要与AsyncTask一起使用吗?我是android的初学者,所以我知道的不多。请帮帮我。或者如果你能提供一个更好的算法,请这样做

像这样使用BufferedReader对象读取

    public JSONObject getJSONFromUrl(String url) {
    InputStream is = null;
    JSONObject jObj = null;
    String json = "";

    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        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();
    } 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;

}

试试这个

它不一定是一个异步任务,但它必须是除主线程(UI运行的地方)之外的其他任务。因此,您也可以使用线程而不是异步任务

否则,4.0之后的android版本将抛出“NetworkOnMainThread”异常。
有关更多详细信息,请参阅。

我认为您通过调用HttpPost方法请求服务器响应,该方法是错误的除非您正在向服务器发布内容,相反,您应该调用HttpGet方法从服务器获取内容 像这样

try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line); // Don't use \n this will make your json object invalid
    }
    is.close();
    json = sb.toString();
} catch (Exception e) {
    Log.e("Buffer Error", "Error converting result " + e.toString());
}
调用web服务时必须使用异步任务,因为不能在主线程上进行Http调用。您应该使用另一个线程来调用长时间运行的操作


有关异步任务的更多信息,请参见

是的,您是对的。您应该使用异步任务从服务器获取数据-

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Accept", "application/json");
        httpGet.setHeader("Content-Type", "application/json");
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        String ss = EntityUtils.toString(httpEntity);
        System.out.println("response" + ss);

谢谢

很难猜测您的问题,因为您没有发布任何日志。但在这里,我向您提供了获取JSON响应的代码,我已经在我的一个应用程序中使用了JSON响应,该响应对我来说非常顺利。我希望它也能对你起作用

new getData().excute();

你能发布堆栈跟踪吗?你面临什么错误?无错误=无帮助。您是否从服务器收到任何json形式的响应?请检查我的答案,确保它适用于您。。
new getData().excute();
public static String parseJSON(String p_url) {
        JSONObject jsonObject = null;
        String json = null;
        try {
            // Create a new HTTP Client
            DefaultHttpClient defaultClient = new DefaultHttpClient();
            // Setup the get request
            HttpGet httpGetRequest = new HttpGet(PeakAboo.BaseUrl + p_url);
            System.out.println("Request URL--->" + PeakAboo.BaseUrl + p_url);
            // Execute the request in the client
            HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
            // Grab the response
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpResponse.getEntity().getContent(), "UTF-8"));
            json = reader.readLine();
            System.err.println("JSON Response--->" + json);
            // Instantiate a JSON object from the request response
            jsonObject = new JSONObject(json);

        } catch (Exception e) {
            // In your production code handle any errors and catch the
            // individual exceptions
            e.printStackTrace();
        }
        return jsonObject;
    }