在Android中使用的参数后使用的HttpClient和HttpPost参数、Android、HttpPost、HttpClient

由网友(繁华散尽终成回忆╮)分享简介:我在写$ C $下一个Android应用程序,应该采取数据包作为JSON和它发布到Web服务器,这又是应该使用JSON回应。I'm writing code for an Android application that is supposed to take data, package it as Json and...

我在写$ C $下一个Android应用程序,应该采取数据包作为JSON和它发布到Web服务器,这又是应该使用JSON回应。

I'm writing code for an Android application that is supposed to take data, package it as Json and post it to a web server, that in turn is supposed to respond with json.

使用GET请求工作正常,但由于某种原因,使用POST的所有数据似乎得到剥离和服务器没有收到任何东西。

Using a GET request works fine, but for some reason using POST all data seems to get stripped and the server does not receive anything.

下面是的code的一个片段:

Here's a snippet of the code:

HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);        
DefaultHttpClient httpClient = new DefaultHttpClient(params);
BasicCookieStore cookieStore = new BasicCookieStore();
httpClient.setCookieStore(cookieStore);

String uri = JSON_ADDRESS;
String result = "";
String username = "user";
String apikey = "something";
String contentType = "application/json";

JSONObject jsonObj = new JSONObject();

try {
    jsonObj.put("username", username);
    jsonObj.put("apikey", apikey);
} catch (JSONException e) {
    Log.e(TAG, "JSONException: " + e);
}

HttpPost httpPost = new HttpPost(uri);
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("json", jsonObj.toString()));
HttpGet httpGet = null;
try {
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
    entity.setContentEncoding(HTTP.UTF_8);
    entity.setContentType("application/json");
    httpPost.setEntity(entity);

    httpPost.setHeader("Content-Type", contentType);
    httpPost.setHeader("Accept", contentType);
} catch (UnsupportedEncodingException e) {
    Log.e(TAG, "UnsupportedEncodingException: " + e);
}

try {
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();

    if (httpEntity != null) {
        InputStream is = httpEntity.getContent();
        result = StringUtils.convertStreamToString(is);
        Log.i(TAG, "Result: " + result);
    }
} catch (ClientProtocolException e) {
    Log.e(TAG, "ClientProtocolException: " + e);
} catch (IOException e) {
    Log.e(TAG, "IOException: " + e);
}

return result;

我想我已经跟着就如何创建参数,并张贴他们的一般准则,但显然不是。

I think I have followed the general guidelines on how to create the parameters and post them, but apparently not.

任何帮助或指针的地方,我能找到一个解决方案,是非常欢迎在这一点上(花几个小时实现无后的数据之后曾经发送)。真正的服务器上运行的Tomcat检票,但我也测试了它一个简单的PHP页面上,有没有什么区别。

Any help or pointers to where I can find a solution, are very welcome at this point (after spending a few hours realizing no post data was ever sent). The real server is running Wicket on Tomcat, but I've also tested it out on a simple PHP page, with no difference.

推荐答案

您是否尝试过这样做没有JSON对象并通过了两项basicnamevaluepairs? 同时,它可能是与你的serversettings

have you tried doing it without the JSON object and just passed two basicnamevaluepairs? also, it might have something to do with your serversettings

更新: 这是一块code我用:

Update: this is a piece of code I use:

InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("lastupdate", lastupdate)); 

try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(connection);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.d("HTTP", "HTTP: OK");
    } catch (Exception e) {
        Log.e("HTTP", "Error in http connection " + e.toString());
    }
阅读全文

相关推荐

最新文章