【问题标题】:How to send post data and retrieve the response body in android?如何在android中发送发布数据并检索响应正文?
【发布时间】:2014-03-11 02:07:13
【问题描述】:

代码可以找到,但它没有返回正确的 HTTPRESPONSE,它假设返回成功!作为回应,而是返回一些乱码。这是我的代码,请帮助我,我尝试了很多东西,但没有任何反应,请指教。

       public class PingServerActivity extends Activity {

   private Button btn6;
    //private EditText value;
     protected String name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ping_server);

    btn6 = (Button) findViewById(R.id.btn_6);
    //value = (EditText) findViewById(R.id.textV5);

    pingButton();

}

public void pingButton() {

    btn6.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // this button would send the request to the server
            new PingPostTask().execute(new String[] {name}); 

        }
    });

}

private class PingPostTask extends AsyncTask<String, Void, String>{

     @Override
        protected String doInBackground(String... params)   
        {           
            BufferedReader inBuffer = null;
            String url = "http://ec2-54-243-205-92.compute-1.amazonaws.com/Tests/ping.php";
            String result = "fail";
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost request = new HttpPost(url);
                List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("Password", "EGOT"));

                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
                        postParameters);

                request.setEntity(formEntity);
                HttpResponse response = httpClient.execute(request);//btw, i'm NOT very sure if this will work LOL
                       result = response.toString();

            } catch(Exception e) {
                // Do something about exceptions
                result = e.getMessage();
            } finally {
                if (inBuffer != null) {
                    try {
                        inBuffer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return  result;
        }

        protected void onPostExecute(String result)
        {       
            //textView.setText(page); 
            Toast toast = Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG);
          toast.show();
        }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.ping_server, menu);
    return true;
}

}

【问题讨论】:

    标签: android asynchronous android-asynctask http-post httpresponse


    【解决方案1】:

    你不能简单地将 HTTPResponse 转换为字符串,你需要将它读入输入流或类似的,给

    替换:

    result = response.toString();
    

    result = EntityUtils.toString(response);
    

    去吧。

    【讨论】:

    • 嘿@xBroak 你能解释一下我不明白的地方
    【解决方案2】:

    响应有时不仅是String类型,也可以是二进制,建议你从inputstream中检索内容

    HttpEntitiy entitiy = response.getEntity();
    Inputstream inputstream = entitiy.getContent();
    //do what you want, e.g. convert to string or decode as bitmap
    

    【讨论】:

      猜你喜欢
      • 2011-06-24
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      相关资源
      最近更新 更多