1

making request in AsynckTask using NameValuePair but not getting response

try
                {  
                    HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
                    HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);

                    System.out.println(operation);
                    post = new HttpPost(operation);
                    if(nvp != null)
                    {
                        String temp = "dgdfg";
                        post.setEntity(new UrlEncodedFormEntity(nvp));
                    }
                    response = client.execute(post);
                    entity = response.getEntity();
                    if(entity != null)
                    {
                        result = EntityUtils.toString(entity);
                    }
                    else
                    {
                        Log.d("Response Failed","Response from server is failed");
                    }
                }catch(Exception ex)
                {
                    ex.printStackTrace();
                }

Url

String url = "http://www.xxx.com/service/xxx.asmx/xxxxMethodName";

request parametres like

{
    UserDetails =     {
        DeviceName = "My Phone";
        DeviceToken = 707fc5a77124dd1a485608e8e03d31b708a0359b852df38bb3cc856e28;
        EmailAddress = "admin@xxx.com";
        IsRemember = True;
        Password = "admin@xxx.com";
    };
}

in numvalues need like

 String mm = "{ UserDetails = {DeviceName=\"My Phone\"; DeviceToken = 707fc5a77124dd1a485608e8e03d31b708a0359b852df38bb3cc856e28;EmailAddress=\"admin@xxx.com\";IsRemember = True; Password = \"admin@xxx.com\";  };}";

but not getting here i m getting [...]

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45
  • 1
    Ankit Never use actual web service on the SO. And by the way Web services are only for app and if anyone find your actual WS URL than it will be risky. – Dilip Manek Oct 29 '13 at 10:59

1 Answers1

1

try this method

public static String doPost(String mUrl, ArrayList<String[]> ArrayStr,
            DefaultHttpClient httpClient) {

        HttpPost postMethod = new HttpPost(mUrl);
        InputStream inputStream = null;
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        String DataTest = "";

        //creating name value pair
        for (int i = 0; i < ArrayStr.size(); i++) {
            DataTest += " ITem -" + ArrayStr.get(i)[0] + ":  Value -"
                    + ArrayStr.get(i)[1];
            nameValuePairs.add(new BasicNameValuePair(ArrayStr.get(i)[0],
                    ArrayStr.get(i)[1]));
        }
        //adding values with postmethod
        try {
            System.out.println("Request - >>" + DataTest);
            postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        try {
            HttpResponse response = httpClient.execute(postMethod);
            response.toString();



        //  System.out.println("Response - >>" + str);

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                inputStream = response.getEntity().getContent();

            }
        } catch (Exception e) {
            Log.e("Exception", e.getMessage());
            return "";
        }
        return inputSteamToString(inputStream);

    }
//to convert response to string

public static String inputSteamToString(InputStream is) {

        StringBuffer responseInBuffer = new StringBuffer();
        byte[] b = new byte[4028];
        try {
            for (int n; (n = is.read(b)) != -1;) {
                responseInBuffer.append(new String(b, 0, n));
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String response=new String(responseInBuffer.toString());
        return response;
    }
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • pls check my request parameter its not only array so getting isssue – Ankitkumar Makwana Sep 07 '13 at 06:58
  • i check your service...it's work ...but it gives null at response.getEntity().getContent().. please remove this & work...for more refrensce take a look to this question. http://stackoverflow.com/questions/10168617/android-httpresponse-content-has-been-consumed – Addicted Manish Sep 07 '13 at 09:34
  • comment this line in my code inputStream=response.getEntity().getContent(); and place a log statement. EntityUtils.toString(response.getEntity()); it give some response regarding your service url...i think u should look on it...it gives you some help to sovle this problem – Addicted Manish Sep 07 '13 at 09:47
  • hey dude your service is url is wrong. it would be.. String URL="http://www.swipealert.com/service/swipealert.asmx?op=MemberSelectByEmailAndPassword"; and remainning work is same as till previous comment... – Addicted Manish Sep 07 '13 at 10:00