[英]Send JSON to server not working(android)
對不起我的英語不好。 我有活動注冊,如果用戶單擊“注冊”,我必須將json發送到服務器。 在我擁有舊產品之前,我像這樣發送json:
ArrayList<NameValuePair> postInform = new ArrayList<NameValuePair>();
postInform.add(new BasicNameValuePair("php string", "my value"));
try{
operationLink.makeHttpRequest(registrationURL, "POST", postInform);
}catch(Exception e) {}
但是現在我有了新的產品,但它不起作用。 我從服務器收到錯誤消息:
{"message":"Customer data is empty!","status":"error"}
在文檔服務器中,我有示例:
JSON:JSON注冊
{"company_id":"1","phones":["380000505050"],"photo":"/files/clients_photos/tmp/484629825.JPG","name":"sdfsdfdsf","birthdate":"10.02.2014","email":"sdf@sdf.ff","cars":{"1":{"car_brand_id":"9","car_model_id":"856","number":"AE5884AH","photo":"/files/clients_photos/tmp/484629824.JPG"}}}
我的程序創建了asynkTask並發送數據:
private String registrationURL = "http://crm.pavlun.info/api/register"; //working link
//code
protected Void doInBackground(String... params) {
JSONParser operationLink = new JSONParser();
ArrayList<NameValuePair> postInform = new ArrayList<NameValuePair>();
postInform.add(new BasicNameValuePair("company_id", "1"));
postInform.add(new BasicNameValuePair("phones", "380950466589"));
postInform.add(new BasicNameValuePair("name", "Alexy"));
postInform.add(new BasicNameValuePair("birthdate", "12.03.2014"));
postInform.add(new BasicNameValuePair("email", "nesalexy@mail.ru"));
postInform.add(new BasicNameValuePair("photo", "/files/clients_photos/tmp/484629825.JPG"));
JSONObject registration = null;
try {
Log.e("perform link", postInform.toString());
registration = operationLink.makeHttpRequest(registrationURL, "POST", postInform);
Log.e("Link", registration.toString());
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
如果我使用像這樣的在線json發送服務器http://gurujsonrpc.appspot.com/ ,則在鏈接中輸入: http://crm.pavlun.info/api/register
: http://crm.pavlun.info/api/register
在“請求JSON字符串”中,我將其放置
{"company_id":"1","phones":["380034505050"],"photo":"/files/clients_photos/tmp/484629825.JPG","name":"sdfsdfdsf","birthdate":"10.02.2014","email":"sdf@sdf.ff"}
我的網站錯誤發送請求時出錯。 我在我的代碼中做錯了什么???
它是我的JSONParser operationLink = new JSONParser();
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) throws JSONException {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
//return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
}
}
我嘗試了您在問題中提供的兩個請求示例,它們都給了我以下答復:
HttpStatus:200 {“ status”:“錯誤”,“消息”:“客戶數據為空!”}
因此,您的問題並不出在您身邊,似乎您缺少一些字段,因此盡管httpStatus不應為200,服務仍會引發錯誤。
我使用chrome中的Advanced Rest Client作為發出其余請求的工具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.