HttpPost无法完成其功能

时间:2012-04-07 19:31:12

标签: android http post try-catch progressdialog

我正在尝试使用HttpPost发送带有一些参数的URL,但它没有成功。我正在使用此代码:

public void Exchange()
{
    final ProgressDialog pd = ProgressDialog.show(this, null, ResourceBundle.getBundle("lang").getString("doTheAction"), true, false);
        new Thread(){
                public void run() {

                    HttpClient httpclient = new DefaultHttpClient();
                    try {
                        httpclient.execute(new HttpPost(URL));
                        Merchant.this.runOnUiThread(new Runnable(){
                            @Override
                                public void run(){
                                    Again();
                                } 
                           });

                    } catch (IOException e) { e.printStackTrace(); }
                    finally { httpclient.getConnectionManager().shutdown(); }
                    pd.dismiss();
                }
        }.start();
}

它不会做任何事情,但它必须更改MySQL数据库上的某些值。有人能帮我吗?这似乎都是正确的。

提前谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用此功能通过API将数据发布到服务器,然后更改MySQL数据库上的某些值是服务器端任务

    public static String post(String to, List <NameValuePair> params) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(to);
            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
            post.setEntity(ent);
            HttpResponse responsePost = client.execute(post);  
            HttpEntity resEntityPost = responsePost.getEntity();  
            if (resEntityPost != null) return EntityUtils.toString(resEntityPost);
        } catch (Exception e) {
            Log.e("POST", e.toString());
        }
        return null;
    }

答案 1 :(得分:0)

您可能想要查看HttpResponse返回的httpClient.execute()。您可能需要检查response.getStatusLine().getStatusCode()可能不是200.您可能还想查看服务器日志是否收到任何内容?你还确定IOException没有被抛出吗?