Android 无法将json发送到服务器
我正在尝试向远程服务器发送一个json文件。如果我尝试使用此网站: 像这样传递json: 它起作用了。但是如果我从我的代码中尝试,我会遇到一些麻烦Android 无法将json发送到服务器,android,json,server,Android,Json,Server,我正在尝试向远程服务器发送一个json文件。如果我尝试使用此网站: 像这样传递json: 它起作用了。但是如果我从我的代码中尝试,我会遇到一些麻烦 ArrayList<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>(); JSONArray list1 = new JSONArray(); list1.add("12345678");
ArrayList<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>();
JSONArray list1 = new JSONArray();
list1.add("12345678");
Map obj=new LinkedHashMap();
obj.put("company_id","1");
obj.put("phones", list1);
obj.put("name","Alexy");
obj.put("birthdate","12.03.2014");
obj.put("email","nesalexy@mail.ru");
nameValuePairs1.add(new BasicNameValuePair("json", obj.toString()));
try {
URL url = new URL("http://crm.pavlun.info/api/register");
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url.toURI());
httpPost.setEntity(new StringEntity(nameValuePairs1.toString(), "UTF-8"));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Encoding", "application/json");
HttpResponse response = httpClient.execute(httpPost);
Log.e("r ", response.toString());
}catch(Exception e) {
e.printStackTrace();
}
也许我的json有问题
UDP:
工作代码
ArrayList<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>();
JSONObject joB = new JSONObject();
JSONArray list1 = new JSONArray();
list1.add("258963147");
Map obj=new LinkedHashMap();
obj.put("company_id","1");
obj.put("phones", list1);
obj.put("name","Alexy");
obj.put("birthdate","12.03.2014");
obj.put("email","nesalexy@mail.ru");
org.json.JSONObject jsonqwe;
try {
JSONParser operationLink = new JSONParser();
ArrayList<NameValuePair> postP = new ArrayList<NameValuePair>();
postP.add(new BasicNameValuePair("json", JSONValue.toJSONString(obj)));
jsonqwe = operationLink.makeHttpRequest("http://crm.pavlun.info/api/register", "POST", postP);
Log.e("sad", jsonqwe.toString());
}catch(Exception e) {
e.printStackTrace();
}
ArrayList nameValuePairs1=新建ArrayList();
JSONObject作业=新建JSONObject();
JSONArray list1=新的JSONArray();
清单1.添加(“258963147”);
Map obj=新建LinkedHashMap();
实物期权(“公司识别号”、“1”);
obj.put(“电话”,列表1);
obj.put(“姓名”、“亚历克斯”);
obj.put(“生日”,“2014年3月12日”);
obj.put(电子邮件)nesalexy@mail.ru");
org.json.JSONObject jsonqwe;
试一试{
JSONParser operationLink=新的JSONParser();
ArrayList post=新的ArrayList();
post.add(新的BasicNameValuePair(“json”,JSONValue.toJSONString(obj));
jsonqwe=operationLink.makeHttpRequest(“http://crm.pavlun.info/api/register","邮递","邮递",;
Log.e(“sad”,jsonqwe.toString());
}捕获(例外e){
e、 printStackTrace();
}
您的问题在于,您不是在构建JSON对象,而是在使用map的toString()
方法,该方法不会为您提供格式正确的JSON对象
尝试JSONObject,它将映射作为参数。然后在JSONObject上调用
toString()
。您的问题是,您不是在构建JSON对象,而是在使用映射的toString()
方法,该方法不会为您提供格式正确的JSON对象
尝试JSONObject,它将映射作为参数。然后在JSONObject上调用
toString()
。此行将返回垃圾(就服务器而言)
因为
ArrayList
确实像您期望的那样实现了toString
。您应该改用JSONArray
/JSONObject
。此行将返回垃圾(就服务器而言)
因为ArrayList
确实像您期望的那样实现了toString
。您应该改用JSONArray
/JSONObject
。尝试更改
Map obj=new LinkedHashMap();
到
您需要发送一个JSON值尝试更改
Map obj=new LinkedHashMap();
到
您需要发送一个JSON值更合适的解决方案是构建一个JSONObject,而不是您正在使用的映射。大概是这样的:
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
JSONArray phoneNumbers = new JSONArray();
phoneNumbers.add("12345678");
JSONObject obj=new JSONObject();
obj.put("company_id","1");
obj.put("phones", phoneNumbers);
obj.put("name","Alexy");
obj.put("birthdate","12.03.2014");
obj.put("email","nesalexy@mail.ru");
nameValuePairs.add(new BasicNameValuePair("json", obj.toString()));
try {
URL url = new URL("http://crm.pavlun.info/api/register");
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url.toURI());
httpPost.setEntity(new StringEntity(nameValuePairs.toString(), "UTF-8"));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Encoding", "application/json");
HttpResponse response = httpClient.execute(httpPost);
Log.e("r ", response.toString());
}catch(Exception e) {
e.printStackTrace();
}
ArrayList nameValuePairs=新的ArrayList();
JSONArray phoneNumbers=新的JSONArray();
电话号码。添加(“12345678”);
JSONObject obj=新的JSONObject();
实物期权(“公司识别号”、“1”);
obj.put(“电话”,电话号码);
obj.put(“姓名”、“亚历克斯”);
obj.put(“生日”,“2014年3月12日”);
obj.put(电子邮件)nesalexy@mail.ru");
添加(新的BasicNameValuePair(“json”,obj.toString());
试一试{
URL=新URL(“http://crm.pavlun.info/api/register");
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost=newhttppost(url.toURI());
setEntity(新的StringEntity(nameValuePairs.toString(),“UTF-8”);
setHeader(“内容类型”、“应用程序/json”);
setHeader(“接受编码”、“应用程序/json”);
HttpResponse response=httpClient.execute(httpPost);
Log.e(“r”,response.toString());
}捕获(例外e){
e、 printStackTrace();
}
更合适的解决方案是构建一个JSONObject,而不是您正在使用的映射。大概是这样的:
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
JSONArray phoneNumbers = new JSONArray();
phoneNumbers.add("12345678");
JSONObject obj=new JSONObject();
obj.put("company_id","1");
obj.put("phones", phoneNumbers);
obj.put("name","Alexy");
obj.put("birthdate","12.03.2014");
obj.put("email","nesalexy@mail.ru");
nameValuePairs.add(new BasicNameValuePair("json", obj.toString()));
try {
URL url = new URL("http://crm.pavlun.info/api/register");
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url.toURI());
httpPost.setEntity(new StringEntity(nameValuePairs.toString(), "UTF-8"));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Encoding", "application/json");
HttpResponse response = httpClient.execute(httpPost);
Log.e("r ", response.toString());
}catch(Exception e) {
e.printStackTrace();
}
ArrayList nameValuePairs=新的ArrayList();
JSONArray phoneNumbers=新的JSONArray();
电话号码。添加(“12345678”);
JSONObject obj=新的JSONObject();
实物期权(“公司识别号”、“1”);
obj.put(“电话”,电话号码);
obj.put(“姓名”、“亚历克斯”);
obj.put(“生日”,“2014年3月12日”);
obj.put(电子邮件)nesalexy@mail.ru");
添加(新的BasicNameValuePair(“json”,obj.toString());
试一试{
URL=新URL(“http://crm.pavlun.info/api/register");
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost=newhttppost(url.toURI());
setEntity(新的StringEntity(nameValuePairs.toString(),“UTF-8”);
setHeader(“内容类型”、“应用程序/json”);
setHeader(“接受编码”、“应用程序/json”);
HttpResponse response=httpClient.execute(httpPost);
Log.e(“r”,response.toString());
}捕获(例外e){
e、 printStackTrace();
}
在Hull站点示例中,发送一个名称-值对。要发送名称值PAI,您的内容类型应为表单url编码。因此,改变:
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Encoding", "application/json");
到
也许这会有帮助:
httpPost.setHeader("ENCTYPE","multipart/form-data");
编辑:
正如其他人已经说过的,不要使用映射,而是使用JSONObject。然后改变
httpPost.setEntity(new StringEntity(nameValuePairs1.toString()));
致:
在Hull站点示例中,发送一个名称-值对。要发送名称值PAI,您的内容类型应为表单url编码。因此,改变:
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Encoding", "application/json");
到
也许这会有帮助:
httpPost.setHeader("ENCTYPE","multipart/form-data");
编辑:
正如其他人已经说过的,不要使用映射,而是使用JSONObject。然后改变
httpPost.setEntity(new StringEntity(nameValuePairs1.toString()));
致:
你犯了什么错误?您是否在UI线程中运行了代码?是否可以在发送之前打印json?请检查您正在发送的内容
obj.toString()
不是json。而且,nameValuePairs1.toString()
,不起作用。而不是StringEntity
您得到了什么错误?您是否在UI线程中运行了代码?是否可以在发送之前打印json?请检查您正在发送的内容obj.toString()
不是json。而且,nameValuePairs1.toString()
,不起作用。这不是使用map:,而是使用map:,如果我更改了它仍然不起作用(更改nameValuePairs1.add(新的BasicNameValuePairPairPairs1(“json”,obj.toString());添加到nameValuePairs1.add(新的BasicNameValuePairs1.add(“json”,JSONValue.toJSONString(obj)));参见示例1-3-编码JSON对象-使用Wiki中的映射更改nameValuePairs1.add(新建BasicNameValuePairs(“JSON”,obj.toString())
到nameValuePairs1.add(新建BasicNameValuePairs(“JSON”,JSONV