【发布时间】:2013-12-10 15:53:21
【问题描述】:
我已经设法将一个包含另一个 JSONOBject 和一个 JSONArray 的 JSONObject 发送到使用 php 解析请求的服务器。
我的问题是,我从服务器收到一个错误代码,它发回错误代码“5”,在这个项目中与“格式错误的 JSON”有关
如果发生此错误,发送到服务器的数据将发布到日志文件,我生成的日志文件是以下信息:
[Lorg.json.JSONObject;@b1fbc8c0
似乎是要字符串化的对象,而不是数据本身。
这是我发送到服务器的 android 代码,我关注了这个网站上的几个帖子并到达了以下内容:
pois 列表定义正确,这不是我认为的问题。
public void uploadToServer(Vector pois) {
try {
JSONObject auth = new JSONObject();
JSONObject walk = new JSONObject();
JSONArray points = new JSONArray();
walk.put("walk name", "TEST NAME");
walk.put("walk desc short", "TEST SHORT");
walk.put("walk desc long", "TEST LONG");
for(int i = 0; i < pois.size(); i ++){
JSONObject location = new JSONObject();
location.put("name", pois.get(i).getName());
location.put("timestamp", 0);
location.put("lattitude",pois.get(i).getLattitude());
location.put("longitude",pois.get(i).getLongitude());
location.put("Description",pois.get(i).getDescription());
points.put(location);
}
auth.put("data", walk);
walk.put("locations", points);
auth.put("authorization","");
auth.put("hash","3b6decebf0bab0e0a08c18a94849d6df1e536d65");
auth.put("salt", "dave");
UploadASyncTask upload = new UploadASyncTask();
upload.execute(auth);
} catch (Exception e) {
e.printStackTrace();
}
}
private class UploadASyncTask extends AsyncTask<JSONObject, Void, Void>{
@Override
protected Void doInBackground(JSONObject...auth) {
try{
HttpParams params = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(params);
HttpPost httpPost = new HttpPost("http://project.chippy.ch/upload.php");
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("data", auth.toString()));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
entity.setContentEncoding(HTTP.UTF_8);
httpPost.setEntity(entity);
httpPost.setHeader("User-Agent", "WalkingTourCreator/1.0");
HttpResponse httpResponse = httpclient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
String result = "";
if(inputStream != null){
result = convertInputStreamToString(inputStream);
}
else{
result = "Did not work!";
}
Log.d("RESULT", result);
}catch(Exception e){
Log.e("ERROR IN SEVER UPLOAD", e.getMessage());
}
return null;
}
}
关于我做错了什么有什么想法吗?
【问题讨论】:
标签: java android json http-post