AsyncTask上的字符串请求

时间:2016-06-27 09:34:38

标签: json android-asynctask

您好我想问一下这里是否有人知道如何在AsyncTask上使用StringRequests。我试过了但是我在PostExecute上收到了错误。

  

D / MyDebug:JSON数组上的错误org.json.JSONException:

的字符0处的输入结束

这是我的代码。

public class MarketsFragment extends Fragment {
private static final String url="http://192.168.1.195/test/test.php";
private static final String STARTING_TEXT ="";
private static String login = null;
private static String debug="MyDebug";

private View rootView;
private ListView lvMarkets;

public static MarketsFragment newInstance(String text)
{
    Bundle args = new Bundle();
    args.putString(STARTING_TEXT,text);
    MarketsFragment marketsHome = new MarketsFragment();
    marketsHome.setArguments(args);
   return marketsHome;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    rootView = inflater.inflate(R.layout.fragment_markets,container,false);

    DBHelper db = new DBHelper(getActivity().getApplicationContext());

    final String c_androidid    =   Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.ANDROID_ID);
    final Cursor rs = db.getID(c_androidid);
    rs.moveToFirst();
    final String c_login = rs.getString(rs.getColumnIndex(DBHelper.c_login));

    AsyncTaskRunner taskRunner = new AsyncTaskRunner();
    taskRunner.execute(c_login);

    return rootView;
}

private class AsyncTaskRunner extends AsyncTask<String,Integer,String>
{
    private ProgressDialog progressDialog;
    String requestString="";
    String c_login;

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Updating Data...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params)
    {
        c_login=params[0];
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response)
            {
                JSONArray jArray = null;
                try
                {
                    jArray = new JSONArray(response);
                    requestString=response;
                    Log.d("MyDebug",response.toString());
                }
                catch (JSONException e)
                {
                    Log.d("MyDebug","AsyncTask onResponse Error: "+e.toString());
                }
            }
        }, new Response.ErrorListener()
            {
                public void onErrorResponse(VolleyError error)
                {
                    Log.d("MyDebug","Volley Error: "+error.toString());
                }
            }){
            @Override
             protected Map<String,String>getParams()
            {
                Map<String,String>params=new HashMap<String,String>();
                params.put("c_login",c_login);
                return params;
            }
        };
        RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
        queue.add(stringRequest);
        return requestString;
    }

    @Override
    protected void onPostExecute(String result)
    {
        try
        {
            JSONArray jsonArray = new JSONArray(result);
            MarketsAdapter marketsAdapter = new MarketsAdapter(getActivity(),jsonArray);
            lvMarkets = (ListView)rootView.findViewById(R.id.lvMarkets);
            lvMarkets.setAdapter(marketsAdapter);
            progressDialog.dismiss();
        }
        catch (JSONException e)
        {
            Log.d("MyDebug","Error on JSON Array "+e.toString());
            progressDialog.setMessage("Error Updating Data. "+e.toString());
            progressDialog.dismiss();
        }

    }
}

}

我希望你能帮助我解决这个问题。

...谢谢

1 个答案:

答案 0 :(得分:0)

<强> 第一

&#34;输入结束于字符0&#34; 。因为这样你得到了空响应,产生了该异常。

如果问题是你得到一个空白的回复,你应该确保在尝试解析它之前从服务器得到一个响应(这是&#34;结果&#34;在onPost()中)

如果问题是一个糟糕的JSON,你应该尝试验证它。 http://jsonlint.com/is非常好检查。

<强> 第二

如果错误:com.android.volley.TimeoutError , 您应该设置请求的RetryPolicy。

见这里..

 protected String doInBackground(String... params)
    {
..................
..............

      int socketTimeout = 30000;//30 seconds - change to what you want

       RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
       RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
       stringRequest.setRetryPolicy(policy);
        queue.add(stringRequest);
        return requestString;

}

<强>第三

使用Volley时,无需将其与AsyncTask结合使用。它在你的另一个线程上做网络的东西。

谢谢..