AsyncTask上的字符串请求



嗨,我想问这里是否有人知道如何在AsyncTask上有StringRequests。我已经尝试过了,但是我在PostExecute上得到了错误。

D/MyDebug: Error on JSON Array org.json.JSONException: End of input at character 0 of

这是我的代码。

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

}

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

谢谢…

第一个

"字符0处输入结束"

如果问题是你得到一个空白的响应,你应该确保你从服务器得到一个响应之前,试图解析它(这是"结果"在onPost())。

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

第二

如果Error: 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;
}

3

当你使用Volley时,没有必要将它与AsyncTask结合使用。它在另一个线程上为你做网络方面的工作。

谢谢. .

相关内容

  • 没有找到相关文章

最新更新