Java 将JSON发送到服务器不工作(android)
对不起我的英语。我有活动注册,若用户单击“注册”,我必须向服务器发送json。在我使用旧prodject之前,然后我发送json,如下所示:Java 将JSON发送到服务器不工作(android),java,php,android,json,Java,Php,Android,Json,对不起我的英语。我有活动注册,若用户单击“注册”,我必须向服务器发送json。在我使用旧prodject之前,然后我发送json,如下所示: ArrayList<NameValuePair> postInform = new ArrayList<NameValuePair>(); postInform.add(new BasicNameValuePair("php string", "my value")); try{ opera
ArrayList<NameValuePair> postInform = new ArrayList<NameValuePair>();
postInform.add(new BasicNameValuePair("php string", "my value"));
try{
operationLink.makeHttpRequest(registrationURL, "POST", postInform);
}catch(Exception e) {}
在docs server中,我有一个示例:
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;
}
发送请求时出现站点错误。
我在代码中做错了什么
它是myJSONParser operationLink=newjsonparser()代码>
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
//函数从url获取json
//通过使用HTTP POST或GET方法
公共JSONObject makeHttpRequest(字符串url、字符串方法、,
列表参数)抛出JSONException{
//发出HTTP请求
试一试{
//检查请求方法
如果(方法==“POST”){
//请求方法为POST
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
//请求方法是GET
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jObj;
//返回新的JSONObject(json.substring(json.indexOf(“{”)、json.lastIndexOf(“}”)+1);
}
}
我尝试了您在问题中提供的两个请求示例,两个请求都给出了以下答复:
HttpStatus:200
{“状态”:“错误”,“消息”:“客户数据为空!”}
因此,您的问题并非来自您这边,似乎您缺少了一些字段,服务因此抛出了一个错误,尽管httpStatus不应为200
我使用chrome中的高级Rest客户端作为工具来发出Rest请求
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;
}
{"company_id":"1","phones":["380034505050"],"photo":"/files/clients_photos/tmp/484629825.JPG","name":"sdfsdfdsf","birthdate":"10.02.2014","email":"sdf@sdf.ff"}
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));
}
}