2012-11-10 54 views
0
一个JSON web服务和处理数据

我试图消耗低于消耗的Android

http://62.253.195.179/disaster/webservices/login.php?message=[{"email":"[email protected]","password":"welcome"}] 

一个web服务这会返回一个JSON阵列

[{"companyuserId":"2","name":"ben stein","superiorname":"Leon","departmentId":"26","departmentname":"Development","companyId":"23","UDID":"12345","isActive":"1","devicetoken":"12345","email":"[email protected]","phone":"5456465465654","userrole":"1","chngpwdStatus":"1"}] 

我的代码如下

try{ 

    String weblink = URLEncoder.encode("http://62.253.195.179/disaster/webservices/login.php?message=[{\"email\":\"[email protected]\",\"password\":\"welcome\"}]"); 
    HttpParams httpParameters = new BasicHttpParams(); 
    int timeoutConnection = 7500; 
    HttpConnectionParams.setConnectionTimeout(httpParameters, 
      timeoutConnection); 
    int timeoutSocket = 7500; 
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
    HttpClient client = new DefaultHttpClient(httpParameters); 
    HttpGet request = new HttpGet(); 
    URI link = new URI(weblink); 
    request.setURI(link); 
    HttpResponse response = client.execute(request); 

    BufferedReader rd = new BufferedReader(new InputStreamReader(
      response.getEntity().getContent())); 
    result = rd.readLine(); 
    JSONObject myData = new JSONObject(result); 
    JSONArray jArray = myData.getJSONArray(""); 
    JSONObject steps = jArray.getJSONObject(0); 
    String name = steps.getString("name"); 

} catch (JSONException e) { 
    e.printStackTrace(); 
} 

但它不工作,我不是100%确定这是做到这一点的最好方法。

11-10 10:49:55.489: E/AndroidRuntime(392): java.lang.IllegalStateException: Target host must not be null, or set in parameters. 

回答

0

你并不需要一个HttpClient

String weblink = URLEncoder.encode("http://62.253.195.179/disaster/webservices/login.php?message=[{\"email\":\"[email protected]\",\"password\":\"welcome\"}]"); 
URI link = new URI(weblink); 
URLConnection linkConnection = link.openConnection(); 
BufferedReader rd = new BufferedReader(new InputStreamReader(linkConnection.getInputStream()); 
.... 

我认为你做的错误是,你用HttpGet request = new HttpGet();代替HttpPost post = new HttpPost(webLink);