【问题标题】:how to write the AsyncTask in JSON parser for my code?如何在 JSON 解析器中为我的代码编写 AsyncTask?
【发布时间】:2015-05-02 19:09:15
【问题描述】:

我正在使用 MySQL 数据库开发登录应用程序。我正在使用数据库包中的 JSONParser 连接到本地 mysql 数据库我收到以下错误,请有人帮助我。我搜索了这个错误,但我得到的是使用 AsyncTask 但我不知道在哪里使用以及如何使用以及主线程也是什么。 请有人编辑我的代码并解释或发布相关代码... “android.os.NetworkonMainThreadException”是我从 4.2 genymotion 模拟器运行应用程序时出现的错误...

package com.android.database;

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) {

        // Making HTTP request
        try {
            // 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();

        } 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;

    }
}``

【问题讨论】:

标签: android mysql eclipse exception android-activity


【解决方案1】:

网络操作必须在后台完成。

您可以使用AsyncTask 来完成此操作。

【讨论】:

    【解决方案2】:

    您正在 UI 线程上执行网络操作,但您无法执行此操作。考虑使用 AsyncTask 来做这种操作,或者像 this 这样的外部库,它可以让你以非常简单的方式异步发出 Http 请求。

    【讨论】:

      【解决方案3】:

      我不确定我是否正确理解了您的问题,我想添加评论但不允许(代表太低)所以这里是:

      我假设这个类 JSONParser 是您正在谈论的“活动”。这不是一个活动,而是一个类,因此您可以创建该类的一个新对象并从您的调用活动中使用它:

      JSONParser json = new JSONParser();
      json.getJSONFromUrl(url,params);
      

      问题是,执行此解析需要时间,并且在主 UI 线程上,这可能会暂停线程,如果线程响应时间超过 5 秒,甚至可能让应用程序崩溃。这意味着您应该使用 AsyncTask 来运行这个 JSONParser 方法。开始学习 AsyncTask 的一个好地方是 Vogella。他的网站上有一些很棒的教程。

      您可能会使用 doInBackground 来运行您的 getJSONFromUrl 方法,然后从 onPostExecute 方法更新您的 UI 线程。所以基本上:使用 AsyncTask

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多