Java 检查POST是否成功(Android)

Java 检查POST是否成功(Android),java,android,post,request,Java,Android,Post,Request,我正在用Android做一个post请求。它应该以字符串的形式给我一个响应。这就是我检查它的方法。然而,它给了我一个空字符串返回。在祝酒词里。我做错什么了吗,有什么提示吗 private void makePostRequest() throws UnsupportedEncodingException { SharedPreferences postpreference = this.getSharedPreferences("preferences", MODE_PRIVATE);

我正在用Android做一个post请求。它应该以字符串的形式给我一个响应。这就是我检查它的方法。然而,它给了我一个空字符串返回。在祝酒词里。我做错什么了吗,有什么提示吗

private void makePostRequest() throws UnsupportedEncodingException {
    SharedPreferences postpreference = this.getSharedPreferences("preferences", MODE_PRIVATE);
    String password = postpreference.getString("Password", null);
    String username = postpreference.getString("Username", null);

    String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
    data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");

    String text = "";
    BufferedReader reader = null;

    try {
        // send post data request
        URL url = new URL("secreturl but working");
        URLConnection conn = url.openConnection();

        OutputStreamWriter streamWriter = new OutputStreamWriter(conn.getOutputStream());

        streamWriter.write(data);
        streamWriter.flush();

        //read the response
        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
            // Append server response in string
            sb.append(line + "\n");
        }
        text = sb.toString();
    } catch (Exception ex) {

    } finally {
        try {
            reader.close();
        } catch (Exception e) {

        }
    }
    // Show response on activity
    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();

}

检查响应代码。然后,只有在获得正确的响应代码时才能获得响应

int responseCode = conn.getResponseCode();

我按照此链接中的第一个解决方案修复了它:

谢谢你的帮助

编辑:执行post请求的正确方式。

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
}
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://www.yoursite.com/script.php");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“stringdata”,“AndDev很酷!”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}

仅仅检查响应代码就足够了吗?可能吧,但这不是问题所在。。。问题是我没有得到响应代码:)我看不到您在哪里发出正确的HTTP请求?@AndroidTestor-您怎么知道的?您的代码甚至没有检查响应代码!提示:
conn.getResponseCode()
Sorry@StephenC,我很快读到了你的评论。在这种情况下,我确实错了。但是,当我连接.getResponseCode()时,应用程序崩溃。我尝试将断点放在调试器的那一行上,但崩溃得太快,甚至无法生成断点。我这样做了,然后尝试在toast中显示,但应用程序崩溃了。这难道不符合逻辑吗?在我看来,它应该显示响应。应用程序崩溃得那么快,甚至没有生成stacktrace。@Glorfindel,刚刚编辑了我的答案:)