我已编写此代码以将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", "adilshafique@hotmail.com");
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;
}
它提供响应,因为方法未定义。服务器的描述如下 网络服务网址
在json要求中发送格式。您需要发送方法发布并将所有json数组分配给客户数组密钥,然后Web服务通过post方法从客户处获取。
即在php
array("customer"=>$jsondata)
然后,Web服务接收此客户帖子值中的所有json数据,发布的字段名称如下:
method => 'login',
email => adilshafique@hotmail.com
password => Adil2014$
submit => Login
第一个是发布的字段名称,第二个是数据。请使用相同的帖子字段名称。
将在我们的数据库中找不到将返回Method undefined,Invalid Password,User ID的消息。
您的帐户尚未激活.Entered主电子邮件存在于数据库中,使用另一个 用户名。
如果成功登录,会话ID将返回
PHP
$url = 'http://juelminapp.visualsparks.net/ws/login';
$data = array('method' = > 'login', 'email' = > 'adilshafique@hotmail.com', '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 :(得分: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", "adilshafique@hotmail.com");
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;
}
这对你有用。希望这有帮助!