[英]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.