在POST方法上将字符串id发送到服务器并获得响应

时间:2015-08-29 14:17:40

标签: android json server http-post

我正在尝试向服务器发送id,在该服务器上将针对该id生成响应,我对该服务器的id是正常的,但响应是空数据数组。

try {
    httppost.setEntity(new StringEntity(vol_id));
    Log.e("vol", vol_id);

    HttpResponse resp = httpclient.execute(httppost);

    HttpEntity ent = resp.getEntity();

    getResult = EntityUtils.toString(ent);
    savePreferences("asset_data", getResult);
} catch (IOException e) {
    e.printStackTrace();
}

1 个答案:

答案 0 :(得分:0)

Try this code with the concept and jar file of multipart entitiy to post string:

Make http request some like as you know:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.tumblr.com/api/write");

Then critical requirement is this to post string over id defined in server:

try {
    MultipartEntity entity = new MultipartEntity();

    entity.addPart("type", new StringBody("photo")); //photo is id for string defined to post
    httppost.setEntity(entity);
    HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}