安卓项目3

这几天一直在为一个问题纠结...这是一个连接服务器的类,提供不同的参数返回不同的结果。

 1 public final class ServiceLink {
 2 /*    
 3     功能:连接服务器
 4     API:link(String url);
 5         get(String bundle, String value);
 6 */
 7     static InputStream is = null;
 8     static JSONObject jObj = null;
 9     static String json = "";           
10     private HttpResponse httpResponse  =null;
11     private    String url = null;
12     HttpPost httpPost = new HttpPost(url);
13 
14     ServiceLink(){
15         Log.i("linkService", "ServiceLink");
16         
17     }
18     public void setURL(String urll){
19         this.url = urll;
20         Log.i("setURL",url);
21     }
22     public JSONObject serviceLogin(String hostname,String username, String password){
23         List<NameValuePair> params = new ArrayList<NameValuePair>();
24         params.add(new BasicNameValuePair("operate","login"));
25         params.add(new BasicNameValuePair("hostname", hostname));
26         params.add(new BasicNameValuePair("username",username));
27         params.add(new BasicNameValuePair("password", password));
28         return putParamsToHttp(params);
29         
30     }
31     public JSONObject serviceClean(){
32         List<NameValuePair> params = new ArrayList<NameValuePair>();
33         params.add(new BasicNameValuePair("operate", "clean"));
34         return putParamsToHttp(params);
35     }
36     
37 
38     public JSONObject putParamsToHttp(List<NameValuePair> params){
39         try {
40             //设置httpPost请求参数
41             httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
42             //
43             httpResponse = new DefaultHttpClient().execute(httpPost);
44             if(httpResponse.getStatusLine().getStatusCode() == 200){
45                    HttpEntity httpEntity = httpResponse.getEntity();
46                     is = httpEntity.getContent();                
47      
48             }
49         } catch (UnsupportedEncodingException e) {
50             // TODO Auto-generated catch block
51             Log.i("linkService", "Link failed");
52         } catch (ClientProtocolException e) {
53             // TODO Auto-generated catch block
54             Log.i("linkService", "Link failed");
55         } catch (IOException e) {
56             // TODO Auto-generated catch block
57             Log.i("linkService", "Link failed");
58         }
59         try {
60                 BufferedReader reader = new BufferedReader(new InputStreamReader(
61                         is, "UTF-8"));
62                 StringBuilder sb = new StringBuilder();
63                 String line = null;
64                 while ((line = reader.readLine()) != null) {
65                     sb.append(line + "\n");
66             }
67                 is.close();
68                 json = sb.toString();
69         } catch (Exception e) {
70                 Log.e("Buffer Error", "Error converting result " + e.toString());
71                 Log.d("json", json.toString());
72         }
73 
74             // try parse the string to a JSON object
75         try {
76                 jObj = new JSONObject(json);
77         } catch (JSONException e) {
78                 Log.e("JSON Parser", "Error parsing data " + e.toString());
79         }
80 
81             // return JSON String
82         return jObj;
83 
84     }
85 }

我在activity下写一个按键提交,代码如下

            public void onClick(View view) {
                // TODO Auto-generated method stub
                Log.i("login","reading JSON!");
                String hostnamestring = hostname.getText().toString();
                String usernamestring = username.getText().toString();
                String passwordstring = password.getText().toString();
                JSONObject jsonobject= new JSONObject();
                                ServiceLink servicelink =  new ServiceLink();
                servicelink.setURL(hostnamestring);

                jsonobject = servicelink.serviceLogin(hostnamestring, usernamestring, passwordstring);
                Log.i("login",jsonobject.toString());
            } 

 

但是会出错

 

 

 

好吧...我继续看为什么会这样....郁闷

posted @ 2013-03-07 10:50  改着名儿玩  阅读(176)  评论(0编辑  收藏  举报