当前位置: 代码迷 >> Android >> 运行程序出错奇怪异常,困扰了好久!请大家来帮着看看~
  详细解决方案

运行程序出错奇怪异常,困扰了好久!请大家来帮着看看~

热度:130   发布时间:2016-05-01 22:25:31.0
运行程序出错奇怪错误,困扰了好久!请大家来帮着看看~~
Java code
package com.studio.android.chp07.ex02;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.TextView;public class HttpPostConnection extends Activity {     private boolean isAuthenticated;     private ArrayList<BasicNameValuePair> pairs;     private DefaultHttpClient httpclient;     private HttpPost httppost;     private InputStream content;     private String returnConnection;         @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        TextView tv = new TextView(this);         Map<String,String> vars = new HashMap<String,String>();        /*post请求的内容mydata="Hello, Android"*/         vars.put("mydata", "Hello, Android");        parameterHttp("http://5billion.com.cn/post.php", vars);                doPost();        tv.setText(this.returnConnection);        this.setContentView(tv);    }          public void parameterHttp(String url, Map<String,String> variables) {          this.httpclient = new DefaultHttpClient();          this.httppost = new HttpPost(url);          this.pairs = new ArrayList();          if(variables != null){               Set keys = variables.keySet();               for(Iterator i = keys.iterator(); i.hasNext();) {                    String key = (String)i.next();                    pairs.add(new BasicNameValuePair(key, variables.get(key)));               }          }     }          public String doPost(){          try {              UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, "ISO-8859-1");                        /** 将POST数据放入http请求*/              httppost.setEntity(p_entity);                        /** 发出实际的HTTP POST请求 */              HttpResponse response = httpclient.execute(httppost);              HttpEntity entity = response.getEntity();              content = entity.getContent();              this.returnConnection = convertStreamToString(content);              Log.d("HttpPostConnection",">>>>>>>>>>>>>>> " + returnConnection);              int status_code = response.getStatusLine().getStatusCode();              if (status_code >= 300) {                  this.isAuthenticated = false;              } else {                  this.isAuthenticated = true;              }          } catch (UnsupportedEncodingException uee) {              // 异常处理          } catch (IOException ioe) {              // 异常处理          } catch(IllegalStateException ise) {              // 异常处理          }          return returnConnection;     }          public String convertStreamToString(InputStream is) {          BufferedReader reader = new BufferedReader(new InputStreamReader(is));          StringBuilder sb = new StringBuilder();          String line = null;          try {               while ((line = reader.readLine()) != null) {                    sb.append(line + "\n");               }          } catch (IOException e) {               e.printStackTrace();          } finally {               try {                    is.close();               } catch (IOException e) {                    e.printStackTrace();               }          }          return sb.toString();     }     public boolean isAuthenticated() {          return isAuthenticated;     }}
  相关解决方案