2014-03-29 58 views
0

发送JSON来服务器我写此代码为发送JSON到服务器错误在机器人

protected String doInBackground(Integer...params) { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://juelminapp.visualsparks.net/ws/login"); 
    try { 
     // Add your data 
     JSONObject obj = new JSONObject(); 
     obj.accumulate("method", "login"); 
     obj.accumulate("email", "[email protected]"); 
     obj.accumulate("password", "Adil2014$"); 
     obj.accumulate("submit", "Login"); 
     JSONObject o2 = new JSONObject(); 
     JSONArray array = new JSONArray(); 
     array.put(obj); 
     o2.put("customer", array); 
     StringEntity entity = new StringEntity(o2.toString()); 
     httppost.setEntity(entity); 
     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 
     InputStream is = response.getEntity().getContent(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
     data = br.readLine(); 
     br.close(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } catch (Exception e) {} 
    return null; 
} 

它给作为方法没有定义的响应。服务器的descrption是遵循 Web服务URL

http://juelminapp.visualsparks.net/ws/login

在JSON要求发送格式。您需要发送方法帖子,并将所有json数组分配给客户数组密钥,然后web服务通过post方法从客户获取它。

在PHP

array("customer"=>$jsondata) 

然后Web服务接收所有的JSON数据在这个客户岗位价值张贴的字段名称是如下:

method => 'login', 
email => [email protected] 
password => Adil2014$ 
submit => Login 

同它的第一张贴字段名第二是数据。请使用相同的帖子栏位名称。

将返回的消息方法未定义,无效密码,用户ID在我们的数据库中找不到。

您的帐户尚未激活。输入主电子邮件存在于数据库中,请使用其他 用户名。

在成功登录的情况下,会话ID将返回

PHP

$url = 'http://juelminapp.visualsparks.net/ws/login'; 
$data = array('method' = > 'login', 'email' = > '[email protected]', 'password' = > 'Adil2014$', 'submit' = > 'Login'); 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
$data_string = urlencode(json_encode($data)); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer" = > $data_string)); 
$result = curl_exec($ch); 
curl_close($ch); 

回答

0

问题是你要发送一个简单的HTTP实体,当服务器被期待作为多形式的数据。试试这个,

protected String doInBackground(Integer...params) { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://juelminapp.visualsparks.net/ws/login"); 
    try { 
     // Add your data 
     JSONObject obj = new JSONObject(); 
     obj.accumulate("method", "login"); 
     obj.accumulate("email", "[email protected]"); 
     obj.accumulate("password", "Adil2014$"); 
     obj.accumulate("submit", "Login"); 
     String urlEncodedData = URLEncoder.encode(obj.toString(), "utf-8"); 

     // Changed Here to MultiPartEntity 

     MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
     reqEntity.addPart("customer", new StringBody(urlEncodedData)); 
     httppost.setEntity(reqEntity); 


     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 
     InputStream is = response.getEntity().getContent(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
     data = br.readLine(); 
     br.close(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } catch (Exception e) {} 
    return null; 
} 

这应该适合你。希望这可以帮助!