如何将数据附加到Android中的帖子网址?

时间:2015-01-29 12:48:43

标签: android json web-services

我收到错误。对象正在成功构建,但它没有附加到URL。请帮忙。我的代码有什么问题?提前致谢。

尝试{

                 String checkin = edit_message.getText().toString();
                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost("http://192.168.0.254/nepadeals/androidweb/checkin");
                 Log.v(TAG, "postURL: " + httppost);

                        List<NameValuePair> data = new ArrayList<NameValuePair>(5);
                        data.add(new BasicNameValuePair("merchant_id", "02"));
                        data.add(new BasicNameValuePair("merchant_location_id", "03"));
                        data.add(new BasicNameValuePair("user_id", "04"));
                        data.add(new BasicNameValuePair("merchant_kiosk_id", "04"));
                        data.add(new BasicNameValuePair("subscriber_phone", checkin));
                        httppost.setEntity(new UrlEncodedFormEntity(data));
             //         httpclient.execute(httppost);
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity resEntity = response.getEntity();  
                        if (resEntity != null) {                               
                            String responseStr = EntityUtils.toString(resEntity).trim();
                            Log.v(TAG, "Response: " +  responseStr);
                            Toast.makeText(CheckinActivity.this,  responseStr, Toast.LENGTH_LONG).show(); 
                            // you can add an if statement here and do other actions based on the response
                        }               
                        edit_message.setText(""); //reset the message text field
                  //      Toast.makeText(getBaseContext(),"Sent",Toast.LENGTH_SHORT).show();
                        Toast.makeText(CheckinActivity.this, "Data: " +data,Toast.LENGTH_LONG).show();

                 } catch (ClientProtocolException e) {
                     e.printStackTrace();
                 } catch (IOException e) {
                     e.printStackTrace();
                 } catch (Throwable t) {
                     Toast.makeText(CheckinActivity.this, "Request failed: " + t.toString(),
                             Toast.LENGTH_LONG).show();
                 }

            }

1 个答案:

答案 0 :(得分:0)

您必须定义内容类型。你不能把数据放在外面设置它的类型。

所以替换

httppost.setEntity(new UrlEncodedFormEntity(data));

httppost.setHeader(HTTP.CONTENT_TYPE,"application/x-www-form-urlencoded;charset=UTF-8");
httppost.setEntity(new UrlEncodedFormEntity(data , "UTF-8"));

我希望这会奏效。未经测试b'z我没有功能和所有设置。