Issue
I got this code elswhere on stackoverflow. It did not work for me from the beginning.
To stop initial errors I had to change
protected void doInBackground(String... params) {
to
protected String doInBackground(String... params) {
I keep returning an error but e.getmessage returns a null value.
public class CallAPI extends AsyncTask<String, String, String> {
public CallAPI(){
//set context variables if required
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String urlString = params[0]; // URL to call
String data = params[1]; //data to post
OutputStream out = null;
Log.i("doinbackground url=", urlString + data);
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
out = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter(out, "UTF-8"));
writer.write(data);
writer.flush();
writer.close();
out.close();
urlConnection.connect();
} catch (Exception e) {
String ErrorMessageJ = "Error message is NULL";
if (e.getMessage() != null ){ErrorMessageJ = e.getMessage();}
Log.i("doinbackground e", ErrorMessageJ);
}
return null;
}
}
Solution
You are returning null
in your doInBackground
check it
may be you need to Return
some String
in your doInBackground
as per params of your AsyncTask
Answered By - AskNilesh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.