Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当多次调用带解析器的AsyncTask时,com.google.gson.JsonSyntaxException_Android_Parsing_Android Asynctask_Gson - Fatal编程技术网

Android 当多次调用带解析器的AsyncTask时,com.google.gson.JsonSyntaxException

Android 当多次调用带解析器的AsyncTask时,com.google.gson.JsonSyntaxException,android,parsing,android-asynctask,gson,Android,Parsing,Android Asynctask,Gson,我使用AsyncTask从后端服务器检索数据,并使用gson解析器对其进行解析。当我多次执行AsyncTask时,我会得到com.google.gson.JsonSyntaxException 这是我的异步解析器 提前感谢。您误用了对AsyncTask类的调用,必须实例化该类,然后定义参数,连接并最终获取JSON对象。最后记住使用motodoonpostExecute()作为输出。你不可以 result = new ParseTask().execute(url).get(); 您必须执行

我使用AsyncTask从后端服务器检索数据,并使用gson解析器对其进行解析。当我多次执行AsyncTask时,我会得到com.google.gson.JsonSyntaxException

这是我的异步解析器


提前感谢。

您误用了对AsyncTask类的调用,必须实例化该类,然后定义参数,连接并最终获取JSON对象。最后记住使用motodoonpostExecute()作为输出。你不可以

result = new ParseTask().execute(url).get();
您必须执行以下操作。这个代码对我来说很好用

private class GetArticulosParam extends AsyncTask<String, Void, Void> {
    public GetArticulosParam() {
        contactList = new ArrayList<HashMap<String, String>>();
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Por Favor, Espere...");
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected Void doInBackground(String... arg0) {

        // String
        // url1="http://pracsysonline.dyndns.org/ListaArticulos/consultaParam.php";
        String url1 = "http://"+direccion+"/ListaArticulos/consultaParam.php";
        String param = arg0[0];
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("param", param));
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        String jsonStr =sh.makeServiceCall(url1,ServiceHandler.POST,params);
        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
                // Getting JSON Array node
                contacts = new JSONArray(jsonStr);
                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);
                    String id = c.getString(TAG_DESC);
                    String name = c.getString(TAG_CODE);

                    HashMap<String, String> contact = new HashMap<String, String>();

                    Log.e("PRUEBA", img);
                    // adding each child node to HashMap key => value
                    contact.put(TAG_DESC, id);
                    contact.put(TAG_CODE, name);
                    contactList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        miAdapter = new ListViewAdapter(MainActivity.this, contactList);
        setListAdapter(miAdapter);
        pDialog.dismiss();
    }
}
私有类GetArticulosParam扩展异步任务{
公共药物{
contactList=新的ArrayList();
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainActivity.this);
pDialog.setMessage(“Por-Favor,Espere…”);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(字符串…arg0){
//串
//url1=”http://pracsysonline.dyndns.org/ListaArticulos/consultaParam.php";
字符串url1=“http://“+direccion+”/ListaArticulos/consultaParam.php”;
字符串参数=arg0[0];
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“参数”,参数));
//创建服务处理程序类实例
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url1,ServiceHandler.POST,params);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
//获取JSON数组节点
联系人=新的JSONArray(jsonStr);
//通过所有触点循环
对于(int i=0;ivalue
联系人:put(标签描述,id);
联系人:put(标签号、姓名);
联系人列表。添加(联系人);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Log.e(“ServiceHandler”,“无法从url获取任何数据”);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
miAdapter=新的ListViewAdapter(MainActivity.this,contactList);
setListAdapter(miAdapter);
pDialog.disclose();
}
}
然后是ServiceHandler类

public class ServiceHandler {

static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * */
public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * @params - http request params
 * */
public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                UrlEncodedFormEntity encodedEntity = new UrlEncodedFormEntity(params,"UTF-8");
                encodedEntity.setContentEncoding(HTTP.UTF_8);
                httpPost.setEntity(encodedEntity);
                //httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);

        }
        //codigo pruebna
        response = new Scanner(httpResponse.getEntity().getContent(),"UTF-8").useDelimiter("\\A").next();
        /*httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity,"utf-8");*/

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;

}
公共类ServiceHandler{
静态字符串响应=null;
公共最终静态int GET=1;
公共最终静态int POST=2;
公共服务处理程序(){
}
/*
*拨打服务电话
*@url-发出请求的url
*@method-http请求方法
* */
公共字符串makeServiceCall(字符串url,int方法){
返回此.makeServiceCall(url,方法,null);
}
/*
*拨打服务电话
*@url-发出请求的url
*@method-http请求方法
*@params-http请求参数
* */
公共字符串makeServiceCall(字符串url,int方法,
列表参数){
试一试{
//http客户端
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpEntity HttpEntity=null;
HttpResponse HttpResponse=null;
//检查http请求方法类型
if(方法==POST){
HttpPost HttpPost=新的HttpPost(url);
//添加post参数
如果(参数!=null){
UrlEncodedFormEntity encodedEntity=新的UrlEncodedFormEntity(参数,“UTF-8”);
setContentEncoding(HTTP.UTF_8);
httpPost.setEntity(编码身份);
//setEntity(新的UrlEncodedFormEntity(参数,“UTF-8”);
}
httpResponse=httpClient.execute(httpPost);
}else if(方法==GET){
//将参数附加到url
如果(参数!=null){
String paramString=URLEncodedUtils
.格式(参数“utf-8”);
url+=“?”+参数字符串;
}
HttpGet HttpGet=新的HttpGet(url);
httpResponse=httpClient.execute(httpGet);
}
//科迪戈·普鲁布纳
response=new Scanner(httpResponse.getEntity().getContent(),“UTF-8”).useDelimiter(“\\A”).next();
/*httpEntity=httpResponse.getEntity();
响应=EntityUtils.toString(httpEntity,“utf-8”)*/
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回响应;
}

}

如果错误使用了对AsyncTask类的调用,则必须实例化该类,然后定义参数,连接并最终获取JSON对象。最后记住使用motodoonpostExecute()作为输出。你不可以

result = new ParseTask().execute(url).get();
您必须执行以下操作。这个代码对我来说很好用

private class GetArticulosParam extends AsyncTask<String, Void, Void> {
    public GetArticulosParam() {
        contactList = new ArrayList<HashMap<String, String>>();
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Por Favor, Espere...");
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected Void doInBackground(String... arg0) {

        // String
        // url1="http://pracsysonline.dyndns.org/ListaArticulos/consultaParam.php";
        String url1 = "http://"+direccion+"/ListaArticulos/consultaParam.php";
        String param = arg0[0];
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("param", param));
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        String jsonStr =sh.makeServiceCall(url1,ServiceHandler.POST,params);
        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
                // Getting JSON Array node
                contacts = new JSONArray(jsonStr);
                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);
                    String id = c.getString(TAG_DESC);
                    String name = c.getString(TAG_CODE);

                    HashMap<String, String> contact = new HashMap<String, String>();

                    Log.e("PRUEBA", img);
                    // adding each child node to HashMap key => value
                    contact.put(TAG_DESC, id);
                    contact.put(TAG_CODE, name);
                    contactList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        miAdapter = new ListViewAdapter(MainActivity.this, contactList);
        setListAdapter(miAdapter);
        pDialog.dismiss();
    }
}
私有类GetArticulosParam扩展异步任务{
公共药物{
contactList=新的ArrayList();
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainActivity.this);
pDialog.setMessage(“Por-Favor,Espere…”);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(字符串…arg0){
//串
//url1=”http://pracsysonline.dyndns.org/ListaArti
public class ServiceHandler {

static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * */
public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * @params - http request params
 * */
public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                UrlEncodedFormEntity encodedEntity = new UrlEncodedFormEntity(params,"UTF-8");
                encodedEntity.setContentEncoding(HTTP.UTF_8);
                httpPost.setEntity(encodedEntity);
                //httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);

        }
        //codigo pruebna
        response = new Scanner(httpResponse.getEntity().getContent(),"UTF-8").useDelimiter("\\A").next();
        /*httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity,"utf-8");*/

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;

}
protected void onPostExecute(JsonObject json) {
super.onPostExecute(json);
result=json;