简体   繁体   中英

Why I'm not getting any response from server in this code

 public  String POST(String url){
    InputStream inputStream = null;
    String result = "";
    try {

        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);

        String json = "";

        // 3. build jsonObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("UserName","example@gmail.com");
        jsonObject.accumulate("Password","123456!");


        // 4. convert JSONObject to JSON to String
        json = jsonObject.toString();

        // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
        // ObjectMapper mapper = new ObjectMapper();
        // json = mapper.writeValueAsString(person); 

        // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpPost.setEntity(se);

        // 7. Set some headers to inform server about the type of the content   
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        // 8. Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);

        Toast.makeText(getBaseContext()," text",Toast.LENGTH_SHORT).show();
        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // 10. convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    // 11. return result
    return result;
}
 private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

     // Toast.makeText(getBaseContext(), "post invoked", Toast.LENGTH_SHORT).show();
        return POST(urls[0]);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getBaseContext(), "Data Sent!"+result+" don", Toast.LENGTH_LONG).show();
   }
}

I tried to invoke this POST method from an object of HTTPAsyncTask class, with targeted url passed. But the Toast execeuted in OnPostExecute() popping up with the message "Data sent!Done", which tells that value of 'result' variable is null. And in log cat it is showing"I'm getting feedback from logcat that "Request time failed,Java.net.socketException:Addres family not supported by protocol". What would the fault? Could anyone please fix this***

Try adding this to AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And please catch this exception:

catch(SocketException se)
       {
         Log.e("Exception ---- : " , "Error on execute??" + se.getMessage());
           se.printStackTrace();
       }

The URL is lacking the protocol part. Try

http://devcare.dyndns.info:85/WCFServices/UserAuthServices.svc/Login

otherwise devcare.dyndns.info will be mistaken as the protocol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM