JOHN JOHNNNY

JOHN JOHNNNY

  • NA
  • 190
  • 134.8k

Error java PostResponseAsyncTask

Jan 31 2016 3:20 PM
Hi
I am developing an android app that uses mysql,php and async for login and registration
when i test the app in the emulator it clicking on the login button it show the error below

at com.mobile.gemstouch.andasynclogin.PostResponseAsyncTask.onPostExecute(PostResponseAsyncTask.java:186)
at com.mobile.gemstouch.andasynclogin.PostResponseAsyncTask.onPostExecute(PostResponseAsyncTask.java:26)

see my code below
public class PostResponseAsyncTask extends AsyncTask<string,> {

private ProgressDialog progressDialog;

private AsyncResponse delegate;
private Context context;
private HashMap<string,> postData = new HashMap<string,>();
private String loadingMessage = "Loading...";
private boolean showLoadingMessage = true;


//Constructor
public PostResponseAsyncTask(Context context, HashMap postData){
this.delegate = delegate;
this.context = context;
}

//Constructor
public PostResponseAsyncTask(Context context,
boolean showLoadingMessage,
AsyncResponse delegate
){
this.delegate = delegate;
this.context = context;
this.showLoadingMessage = showLoadingMessage;
}

public PostResponseAsyncTask(Context context,
HashMap<string,> postData,
AsyncResponse delegate){
this.context = context;
this.postData = postData;
this.delegate = delegate;
}

public PostResponseAsyncTask(Context context,
HashMap<string,> postData,
boolean showLoadingMessage,
AsyncResponse delegate
){
this.context = context;
this.postData = postData;
this.delegate = delegate;
this.showLoadingMessage = showLoadingMessage;
}

public PostResponseAsyncTask(Context context,
String loadingMessage,
AsyncResponse delegate){
this.context = context;
this.loadingMessage = loadingMessage;
this.delegate = delegate;
}

public PostResponseAsyncTask(Context context,
HashMap<string,> postData,
String loadingMessage,
AsyncResponse delegate){
this.context = context;
this.postData = postData;
this.loadingMessage = loadingMessage;
this.delegate = delegate;
}
//End Constructor

@Override
protected void onPreExecute() {
if(showLoadingMessage == true){
progressDialog = new ProgressDialog(context);
progressDialog.setMessage(loadingMessage);
progressDialog.show();
}

super.onPreExecute();
}//onPreExecute

@Override
protected String doInBackground(String... urls){

String result = "";

for(int i = 0; i <= 0; i++){

result = invokePost(urls[i], postData);
}

return result;
}//doInBackground

private String invokePost(String requestURL, HashMap<string,> postDataParams) {

URL url;
String response = "";
try {
url = new URL(requestURL);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));

writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();

if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
response+=line;
}
}
else {
response="";

Log.i("PostResponseAsyncTask", responseCode + "");
}
} catch (Exception e) {
e.printStackTrace();
}

return response;
}//performPostCall

private String getPostDataString(HashMap<string,> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;

for(Map.Entry<string,> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");

result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}

return result.toString();
}//getPostDataString

@Override
protected void onPostExecute(String result) {
if(showLoadingMessage == true){
if(progressDialog.isShowing()){
progressDialog.dismiss();
}
}

result = result.trim();

delegate.processFinish(result);
}//onPostExecute

//Setter and Getter
public String getLoadingMessage() {
return loadingMessage;
}

public void setLoadingMessage(String loadingMessage) {
this.loadingMessage = loadingMessage;
}

public HashMap<string,> getPostData() {
return postData;
}

public void setPostData(HashMap<string,> postData) {
this.postData = postData;
}

public Context getContext() {
return context;
}

public AsyncResponse getDelegate() {
return delegate;
}

//End Setter & Getter
}
Kindly help