Raven Raven - 5 months ago 14
JSON Question

String Requests on AsyncTask

Hi i would like to ask if someone here knows how to have StringRequests on AsyncTask. I have tried it but i get the error on PostExecute.


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


here is my code.

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

}
}


}

I hope you can help me with this one.

Thanks...

Answer

First

"End of input at character 0".its because of this you got null response which generated that exception.

if the problem is that you're getting a blank response, you should make sure you get a response from the server before trying to parse it (which is "result" in onPost()).

if the problem is a bad JSON, you should try and validate it. http://jsonlint.com/is a very good to check.

Second

And if Error: com.android.volley.TimeoutError , You should set the request's RetryPolicy.

see here..

 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;

}

Third

When you use Volley, there's no need to combine it with AsyncTask. It does the networking stuff on another thread for you.

Thanks..

Comments