1

AsynckTask在使用中发出请求NameValuePair但没有得到响应

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();
                }

网址

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

请求参数如

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

在 numvalues 中需要像

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

但我没有到这里 [...]

4

1 回答 1

1

试试这个方法

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;
    }
于 2013-09-07T06:55:48.147 回答