0

我对 Android 和 Java 还很陌生,下面的代码是通过IllegalStateException. 通过放置打印日志语句,我已经确定代码进入该行:

 "HttpPost post = new HttpPost(string+params)" (makes it to "Point 1")

我无法弄清楚为什么会出错。发送到 HttpPost 的 URL 有效。

请注意,我尝试使用注释掉的 HttpGet 行来代替并且有相同的错误。

public String ReadQuery(String SQL)
    {

        String host = "http://web.engr.illinois.edu/~dyel-net/readquery.php";


        DefaultHttpClient httpclient = new DefaultHttpClient();

        try
        {



        HttpPost httpPost = new HttpPost(host);
        httpPost.addHeader("Accept", "text/plain");
        Log.w("DEBUGGING PRINT", "point 1");
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("user", username));
        nvps.add(new BasicNameValuePair("pw", password));
        nvps.add(new BasicNameValuePair("sql", SQL));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        Log.w("DEBUGGING PRINT", "point 2");
        Log.w("DEBUGGING PRINT", httpPost.getURI().toString());
        Log.w("DEBUGGING PRINT", httpPost.getEntity().toString());
        HttpResponse response = httpclient.execute(httpPost);
        Log.w("DEBUGGING PRINT", "point 3");
        HttpEntity entity = response.getEntity();
        Log.w("DEBUGGING PRINT", "point 4");
        String htmlResponse = EntityUtils.toString(entity);
        Log.w("DEBUGGING PRINT", "point 5");
        return htmlResponse;

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "ERROR";

我的错误:

10-21 13:39:09.472:带调试打印(866):第 1 点 10-21 13:39:09.582:带调试打印(866):第 2 点 10-21 13:39:09.582:带调试打印(866): http ://web.engr.illinois.edu/~dyel-net/readquery.php? 10-21 13:39:09.616:带调试打印(866):org.apache.http.params.BasicHttpParams@4186af10 10-21 13:39:22.792:D/AndroidRuntime(866):关闭 VM 10-21 13:39:22.802: W/dalvikvm(866): threadid=1: 线程以未捕获的异常退出 (group=0x41465700) 10-21 13:39:23.713: E/AndroidRuntime(866): 致命异常: main 10-21 13:39:23.713:E/AndroidRuntime(866):java.lang.IllegalStateException:无法执行活动的方法 10-21 13:39:23.713:E/AndroidRuntime(866):在 android.view.View$1。 onClick(View.java:3633) 10-21 13:39:23.713: E/AndroidRuntime(866): 在 android.view.View.performClick(View.java:4240) 10-21 13:39:23.713: E/ AndroidRuntime(866): 在 android.view.View$PerformClick.run(View.java:17721) 10-21 13:39:23.713: E/AndroidRuntime(866): 在 android.os.Handler.handleCallback(Handler.java :

4

1 回答 1

2
 HttpPost post = new HttpPost(host+params);
        //HttpGet get = new HttpGet(host+params);

这不是使用 HttpClient 发出 POST 请求的正确方法。HTTP GET 请求和 HTTP POST 请求的工作方式不同。在 HTTP GET 请求中,您可以将数据作为 url 的一部分发送,例如,http://mysite.com?feild1=data1&feild2=data2但是 HTTP POST 请求有一个request message body您需要将数据写入服务器的位置。

试试下面的代码:

HttpPost httpPost = new HttpPost("http://targethost/login");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("user", username));
nvps.add(new BasicNameValuePair("pw", password));
nvps.add(new BasicNameValuePair("sql", SQL));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse response = httpclient.execute(httpPost);
于 2013-10-20T23:25:05.553 回答