gpt4 book ai didi

java - 发送 JSON 到服务器不工作(android)

转载 作者:太空宇宙 更新时间:2023-11-04 13:54:12 25 4
gpt4 key购买 nike

抱歉我的英语不好。我有 Activity 注册,如果用户单击“注册”,我必须将 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 在请求 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));

}
}

最佳答案

我尝试了您在问题中提供的两个请求示例,它们都给了我以下响应:

HTTP 状态:200{"status":"error","message":"客户数据为空!"}

所以你的问题不是来自你这边,你似乎缺少一些字段,并且服务因此抛出错误,尽管 httpStatus 不应该是 200。

我使用 Chrome 中的 Advanced Rest Client 作为发出剩余请求的工具。

关于java - 发送 JSON 到服务器不工作(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30002474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com