HttpClient会为以下代码抛出错误

时间:2014-09-17 17:45:40

标签: android

输入流将logcat中的输出显示为“org.apache.http.conn.EofSensorInputStream@527021ec”并输出抛出错误。登录失败或我在代码中犯了任何错误。 Plz帮助...附上下面的logcat图片。

protected Void doInBackground(String... params) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(UrlLink);
    try {
        // Add user name and password

        String username = "xaeroprasad";
        String password = "ramesh88";
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("login", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        Log.v("SENCIDE", "Execute HTTP Post Request");
        HttpResponse response = httpclient.execute(httppost);
        InputStream is=response.getEntity().getContent();
        Log.v("ddd",is.toString());
        String line = "";
         StringBuilder total = new StringBuilder();
         BufferedReader rd = new BufferedReader(new InputStreamReader(is));

         try {
          while ((line = rd.readLine()) != null) { 
            total.append(line); 
          }
          Log.v("dfd",line);
         } catch (IOException e) {
          e.printStackTrace();
         }

Logcat输出:   http://i.stack.imgur.com/rKZgE.png

2 个答案:

答案 0 :(得分:1)

您正在运行循环,直到line为空:

while ((line = rd.readLine()) != null) { 
    total.append(line); 
}

当它结束时(line == null),您打印line

Log.v("dfd",line);

抛出错误,因为Log的第二个参数不能为空。

你可能想在循环后记录total

while ((line = rd.readLine()) != null) { 
    total.append(line); 
}
Log.v("dfd", total);

答案 1 :(得分:0)

try {
  while ((line = rd.readLine()) != null) { 
    total.append(line); 
  }
  Log.v("dfd",total); //<-- Edit this, its a Typo
 } catch (IOException e) {
  e.printStackTrace();
 }