0

I'm trying to post to a server via Asynchronous Task and while converting Hashmap to String, this error occurred. What does that Error mean. Why does it occour ? How do I resolve this ? here is my code:

public class NetworkAccess extends AsyncTask<HashMap<String,String>,Void,String> {
private URL url;
private HttpURLConnection con;
private OutputStream os;

@Override
protected String doInBackground(HashMap<String, String>... vals) {
    String response = "";

    try {
        url = new URL("http://192.168.0.3/chow/user-login.php");
        con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setDoOutput(true);
        con.setConnectTimeout(1000);
        OutputStream os = con.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
        String pd = getPostDataString(vals); //Error
        writer.write(pd);
        writer.flush();
        writer.close();
        BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
        BufferedReader r = new BufferedReader(new InputStreamReader(bis));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line);
        }
        response = total.toString();
    }
    catch (Exception e){
        e.printStackTrace();

    return response;
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for(Map.Entry<String, 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();
}

}

1 Answer 1

0

The error which you got shows that you're directly trying to create a array of HashMap, rather you can create just Array of String, iterate the Collection and insert the HashMap objects into it.

Inside the getPostDataString(), change the code to

StringBuilder[] sb = new StringBuilder[100];

and check if it works.

2
  • It's not working.It is still showing the same error with another error "cannot resolve method append" Nov 17, 2015 at 6:37
  • you're returning a single string object, after iterating the entire collection, if you see it carefully, only the last object in the collection is going to be returned. might you're expected to return every object from the collection after iterate, the place where you're calling the getPostDataString().. Nov 17, 2015 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.