2014-03-29 83 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; 
} 

這應該適合你。希望這可以幫助!