I am working on a project here and am running into an issue. It was working fine before but what I am doing is sending a request to a url and am expecting a JSON response. However when I check the response it keeps coming back as null. This was not an issue before but randomly started happening. Any help would be greatly appreciate!'
Also I have another class that actually does the url requests.
This first bit is the onclick listener on the login page. It takes the text fields and converts them to strings. The way the login works is it has to push all the information into the URL which is why it is set up this way.
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String account = acct.getText().toString();
String uname = user.getText().toString();
String passWord = pass.getText().toString();
url = ap+"&t2mdeveloperid="+id+account+"&t2musername="+uname+"&t2mpassword="+passWord;
new Login().execute();
}
}
);
}
Alright here is the Async task that the request runs in.
private class Login extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute(){
super.onPreExecute();
//Here we will display our progress bar. This is dependent on each page.
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Talk2M Account");
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0){
//We need to create an object to call our service handler to make our http request.
ServiceHandler login = new ServiceHandler();
//Now we need to make a request to the url as well as recieve the response.
String response = login.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + response);
try{
JSONObject jsonObj = new JSONObject(response);
String session = jsonObj.getString(TAG_SESSION);
}catch(JSONException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
if(pDialog.isShowing())
pDialog.dismiss();
}
}
If the JSON you're getting back is null, my first guess would be to verify that you are hitting the right URL. If that is the case, then there may be an issue on the api side. You may want to test this HTTP request outside of the context of your application, in a REST client or web browser.
Regards.