带数组对象的Http请求

时间:2013-09-07 06:49:57

标签: android http

使用AsynckTaskNameValuePair中发出请求但未获得回复

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\";  };}";

但是没有到达这里我得到了[...]

1 个答案:

答案 0 :(得分: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;
    }