0

我正在为我们的 Android 设备创建一个应用程序。本节的目的是将用户名和密码(目前只是作为字符串分配)发布到 Web 服务并接收登录令牌。运行代码时,在 getOutputStream() 行,我的代码将终止并且不会再有任何进展。

我已经分配了 android 模拟器 GSM 访问权限,并在 Eclipse 中设置了代理和 DNS 服务器。我现在不知道该去哪里!

这是在我的 onHandleIntent() 中:

protected void onHandleIntent(Intent i) {
    try{

        HttpURLConnection http_conn = (HttpURLConnection) new URL("http://www.XXXXX.com").openConnection();

        http_conn.setRequestMethod("POST");
        http_conn.setDoInput(true); 
        http_conn.setDoOutput(true); 
        http_conn.setRequestProperty("Content-type", "application/json; charset=utf-8"); 

        String login = URLEncoder.encode("XXXXX", "UTF-8") + "=" + URLEncoder.encode("XX", "UTF-8");
        login += "&" + URLEncoder.encode("XXXXX", "UTF-8") + "=" + URLEncoder.encode("XX", "UTF-8");



        OutputStreamWriter wr = new OutputStreamWriter(http_conn.getOutputStream());
        //TERMINATES HERE
        wr.write(login);
        wr.flush();

        BufferedReader rd = new BufferedReader(new InputStreamReader(http_conn.getInputStream()));
        String line = rd.toString();

        wr.close();
        rd.close();

        http_conn.disconnect();
        }
        catch (IOException e){
        }
}

这是我第一次接触 java 并且只写了几天,所以如果我错过了一些明显的东西,请多多包涵。

谢谢

4

3 回答 3

1
String line = rd.toString();

应该

String line = rd.readLine();

这可能会奏效。rd.toString()给你一个字符串表示你的BufferedReader. 它不会触发 HTTP 操作。我没有测试你的代码,所以可能还有其他错误,这只是一个明显的错误。

于 2012-08-02T12:27:01.403 回答
1

如果您想使用 HTTP 发布内容,为什么不使用 HTTP POST?;-)

这是一个示例片段:

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

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

来源:http ://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

于 2012-08-02T12:22:16.627 回答
1

这可能不是正确的答案,但肯定会对您有所帮助。我已使用此代码向 Web 服务发送和接收请求并回复响应。

此代码正在运行,但需要一些Refactoring,因为我使用了一些不需要的额外变量。

我已经用NameValuePair这里的帖子

public String postData(String url, String xmlQuery) {



        final String urlStr = url;
        final String xmlStr = xmlQuery;
        final StringBuilder sb  = new StringBuilder();


        Thread t1 = new Thread(new Runnable() {

            public void run() {

                HttpClient httpclient = new DefaultHttpClient();


                HttpPost httppost = new HttpPost(urlStr);


                try {

                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                            1);
                    nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpclient.execute(httppost);

                    Log.d("Vivek", response.toString());

                    HttpEntity entity = response.getEntity();
                    InputStream i = entity.getContent();

                    Log.d("Vivek", i.toString());
                    InputStreamReader isr = new InputStreamReader(i);

                    BufferedReader br = new BufferedReader(isr);

                    String s = null;


                    while ((s = br.readLine()) != null) {

                        Log.d("YumZing", s);
                        sb.append(s);
                    }


                    Log.d("Check Now",sb+"");




                } catch (ClientProtocolException e) {

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

        });

        t1.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        System.out.println("Getting from Post Data Method "+sb.toString());

        return sb.toString();
    }
于 2012-08-02T12:23:18.180 回答