【问题标题】:Http request with arry object带有数组对象的 Http 请求
【发布时间】:2013-09-07 06:49:57
【问题描述】:

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

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

【问题讨论】:

  • Ankit 永远不要在 SO 上使用实际的 Web 服务。顺便说一句,Web 服务仅适用于应用程序,如果有人找到您的实际 WS URL,那将是有风险的。

标签: android http


【解决方案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;
    }

【讨论】:

  • 请检查我的请求参数,它不仅是数组,因此会出现问题
  • 我检查了您的服务...它的工作...但它在 response.getEntity().getContent() 处给出 null ..请删除此和工作...更多参考看看对这个问题。 stackoverflow.com/questions/10168617/…
  • 在我的代码中注释这一行 inputStream=response.getEntity().getContent();并放置一个日志语句。 EntityUtils.toString(response.getEntity());它对您的服务网址给出了一些回应...我认为您应该查看它...它为您解决此问题提供了一些帮助
  • 嘿伙计,您的服务是 url 错误。它会是.. String URL="swipealert.com/service/…";剩下的工作与之前的评论相同......
猜你喜欢
  • 2012-10-04
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多