问题描述
我尝试做什么
您好,大家好,我试图创建一个应用程序中,我可以查看客户给我的订单。为此,我创建了我的服务器上的接口上,我可以发送POST /获取/ set请求的。服务器的响应是JSON格式的。 (为您的信息只ATM是dummydata填写)
现在,当我做一个GET请求从我的应用程序到服务器,我从它那里得到的回应却是不完全对响应我应该得到的一半是不存在的! :(但是当我打开在我的浏览器的Get请求的URL,我得到充分的响应。
问
就像你看到它不可能是一个基于服务器的问题,因为我也通过卷曲tryed要做到这一点得到requst,并得到了永诺充分反应。
在我的应用我的DefaultHttpClient工作,所以我因子评分问题的根本是,有一个为响应的限制,但我没有发现它。
所以,我在哪里可以改变这种响应尺寸,还有什么可为什么我没有得到充分反应的问题!一些好的code-片段或者任何你能想象将帮助!
到这里,你会发现它确实获取请求的了Methode的code。
code
如果你需要更多的code,只是把它写在评论!
getOrders()
公共无效getOrders(){
Log.d(DataHandlerService,Aufträgewerden geladen);
线程t =新的Thread(){
公共无效的run(){ 共享preferences设置= getShared preferences(preFS_NAME,0);
字符串userid = settings.getString(用户id,UID);
Log.d(DataHandlerService,用户ID); //下载网址
字符串的URL =http://api.i-v-o.ch/users/用户ID +
+/assignments.json
Log.d(请求的URL,URL); DefaultHttpClient客户端=新DefaultHttpClient();
HTT presponse响应; 尝试{
HTTPGET请求=新HTTPGET();
request.setURI(新的URI(URL));
request.addHeader(内容类型,
应用程序/ x-WWW的形式urlen codeD);
响应= client.execute(请求);
。INT状态code = response.getStatusLine()的getStatus code();
开关(状态code){ 案例200: 如果(响应!= NULL){ StringBuilder的SB =新的StringBuilder();
RD的BufferedReader =新的BufferedReader(
新的InputStreamReader(response.getEntity()
.getContent())); 串线;
而((行= rd.readLine())!= NULL){
sb.append(行+\\ n);
}
字符串结果;
结果= sb.toString();
Log.d(响应,结果);
JSONReader(结果); //这里的JSON将产生
} 打破; 案例500:
//错误处理
打破;
}
}赶上(例外五){
e.printStackTrace();
Log.e(DataHandler的,URLConnection的错误+ E);
} }
};
t.start();
}
下面是你自找的,就像你看到它的一部分是不存在的响应:!
<$p$p><$c$c>[{\"created_at\":\"2012-01-06T17:10:00Z\",\"end_datetime\":\"2008-03-25T13:00:00Z\",\"id\":2127,\"start_datetime\":\"2008-03-25T13:00:00Z\",\"updated_at\":\"2012-01-06T17:10:00Z\",\"title\":\"2127 Foobar的,referee_forename:彼得,referee_surname:Gertsch,referee_full_name:彼得Gertsch,category_title:安装 - SAT,status_title:关闭 - Erfolgreich\"},{\"created_at\":\"2012-01-06T17:10:03Z\",\"end_datetime\":\"2008-04-04T12:00:00Z\",\"id\":2134,\"start_datetime\":\"2008-04-04T12:00:00Z\",\"updated_at\":\"2012-01-06T17:10:03Z\",\"title\":\"2134 Foobar的,referee_forename:丹尼尔,referee_surname:布伦纳,referee_full_name:丹尼尔·布伦纳,category_title:安装 - SAT,status_title:关闭 - Erfolgreich\"},{\"created_at\":\"2012-01-06T17:10:03Z\",\"end_datetime\":\"2008-04-07T12:00:00Z\",\"id\":2136,\"start_datetime\":\"2008-04-07T12:00:00Z\",\"updated_at\":\"2012-01-06T17:10:03Z\",\"title\":\"2136 Foobar的,referee_forename:安德烈亚斯,referee_surname:卢茨,referee_full_name:安德烈亚斯卢茨,category_title:安装 - SAT,status_title:封闭 - technisches problem\"},{\"created_at\":\"2012-01-06T17:10:08Z\",\"end_datetime\":\"2008-05-22T07:00:00Z\",\"id\":2144,\"start_datetime\":\"2008-05-22T07:00:00Z\",\"updated_at\":\"2012-01-06T17:10:08Z\",\"title\":\"2144 Foobar的,referee_forename:帕斯卡,referee_surname:Pichand,referee_full_name:帕斯卡尔Pichand,category_title:安装 - SAT,status_title:关闭 - Erfolgreich\"},{\"created_at\":\"2012-01-06T17:10:08Z\",\"end_datetime\":\"2008-05-15T07:00:00Z\",\"id\":2145,\"start_datetime\":\"2008-05-15T07:00:00Z\",\"updated_at\":\"2012-01-06T17:10:08Z\",\"title\":\"2145 Foobar\",\"referee_forename\":\"Hansruedi\",\"referee_surname\":\"W\ürgler\",\"referee_full_name\":\"Hansruedi W¯¯\\ u00fcrgler,category_title:安装 - SAT,status_title:关闭 - Erfolgreich\"},{\"created_at\":\"2012-01-06T17:10:08Z\",\"end_datetime\":\"2008-05-26T08:00:00Z\",\"id\":2146,\"start_datetime\":\"2008-05-26T08:00:00Z\",\"updated_at\":\"2012-01-06T17:10:08Z\",\"title\":\"2146 Foobar的,referee_forename:玛蒂娜,referee_surname:Issler,referee_full_name:玛蒂娜Issler,category_title:安装 - SAT,status_title:关闭 - Erfolgreich\"},{\"created_at\":\"2012-01-06T17:10:08Z\",\"end_datetime\":\"2008-06-03T14:00:00Z\",\"id\":2147,\"start_datetime\":\"2008-06-03T14:00:00Z\",\"updated_at\":\"2012-01-06T17:10:08Z\",\"title\":\"2147 Foobar的,referee_forename:马蒂亚斯,referee_surname:库恩,referee_full_name:马蒂亚斯库恩,category_title:安装 - SAT,status_title:关闭 - Erfolgreich\"},{\"created_at\":\"2012-01-06T17:10:12Z\",\"end_datetime\":\"2008-07-07T07:00:00Z\",\"id\":2157,\"start_datetime\":\"2008-07-07T07:00:00Z\",\"updated_at\":\"2012-01-06T17:10:12Z\",\"title\":\"2157 Foobar的,referee_forename:埃伯哈德,referee_surname:Polatzek,referee_full_name:埃伯哈德Polatzek,category_title:安装 - SAT,status_title:关闭 - Erfolgreich\"},{\"created_at\":\"2012-01-06T17:10:13Z\",\"end_datetime\":\"2008-07-11T08:00:00Z\",\"id\":2161,\"start_datetime\":\"2008-07-11T08:00:00Z\",\"updated_at\":\"2012-01-06T17:10:13Z\",\"title\":\"2161 Foobar的,referee_forename:马加利,referee_surname:Bohin,referee_full_name:马加利Bohin,category_title:安装 - SAT,status_title:关闭 - Erfolgreich\"},{\"created_at\":\"2012-01-06T17:10:14Z\",\"end_datetime\":\"2008-07-25T08:30:00Z\",\"id\":2163,\"start_datetime\":\"2008-07-25T08:30:00Z\",\"updated_at\":\"2012-01-06T17:10:14Z\",\"title\":\"2163 Foobar的,referee_forename:(Centrum酒店Griesalp),referee_surname:Haltenegg BETRIEBS AG,referee_full_name:(Centrum酒店Griesalp)Haltenegg BETRIEBS AG,category_title:安装 - SAT, status_title:关闭 - Erfolgreich\"},{\"created_at\":\"2012-01-06T17:10:16Z\",\"end_datetime\":\"2008-08-07T09:00:00Z\",\"id\":2170,\"start_datetime\":\"2008-08-07T09:00:00Z\",\"updated_at\":\"2012-01-06T17:10:16Z\",\"title\":\"2170 foobar的,referee_forename:。,referee_surname:SAC Hollandiah \\ u00fctte,referee_full_name:。 SAC Hollandiah \\ u00fctte,category_title:安装 - SAT,status_title:封闭 - Erfolgreich},{created_at:2012-01-06T17:10:16Z,end_datetime:2009年-05-07T06:30:00Z,我啊。对了,这个问题是不是你的连接或类似的东西。您的服务已返回一个数组 - 的不的对象 - 因此,你应该分析它是这样的:
的Htt presponse响应= ...
如果(..验证状态..){
JSONArray阵列=新JSONArray(HttpEntityUtils.toString(response.getEntity()));
//你的JSONArray现在可以一起玩。
}
和考虑使用AsyncTask的,而不是一个线程,就像这样:
类AssignmentsTask扩展的AsyncTask&LT;弦乐,太虚,JSONArray&GT; {
@覆盖
保护JSONArray doInBackground(字符串... PARAMS){
最终字符串URL =http://api.i-v-o.ch/users/+参数[0]
+/assignments.json
尝试{
HTT presponse响应= mClient.execute(新HTTPGET(URL));
如果(response.getStatusLine()的getStatus code()== 200){
返回新JSONArray(EntityUtils.toString(response.getEntity()));
}其他{
Log.w(TAG,错误接收任务的+ PARAMS [0] +,+ response.getStatusLine());
}
}赶上(ClientProtocolException E){
Log.w(TAG,原:错误获取分配为+ PARAMS [0],E);
}赶上(IOException异常五){
e.printStackTrace();
Log.w(TAG,IO:对错误的阅读作业+ PARAMS [0],E);
}赶上(ParseException的E){
Log.w(TAG,:+参数[0],E为解析错误分析任务);
}赶上(JSONException E){
Log.w(TAG,JSON:错误JSON解析为+ PARAMS [0],E);
}
返回null;
} @覆盖
保护无效onPostExecute(JSONArray结果){
//东西,处理所产生的JSONObject上
//在UI线程应该放在这里(即更新查看:秒) //如果操作失败,结果为空
}
}
和来检索为了使用户116:
新AssignmentsTask()执行(116);
What I try to do
Hello Guys, I'm trying to create an App in which I can view the Orders the Customers gave to me. For this I created a interface on my server, on which I can send post/get/set request's. The response of the Server is in JSON-Format. (For your Information atm only dummydata is filled in)
Now when I do a get request from my app to the server, I get a response from it but it isn't complete about the half of the response I should get isn't there! :( But when I open the URL with the Get-Request in my browser, I get the full response.
Question
Like you see it can't be a server-based problem, because I also tryed via 'curl' to do this get requst, and allways got the full response.
In my App i work with the DefaultHttpClient, so I tought the Problem simply could be that there's a limit for the response but I didn't found it.
So where can I change this "response-size" and what else could be the problem why I don't get the full response! Some good code-snippets or whatever you can imagine would help!
Down here you'll find the code of the Methode which does the Get-Request.
Code
If you need more Code, just write it in the comments!
getOrders()
public void getOrders() {
Log.d("DataHandlerService", "Aufträge werden geladen");
Thread t = new Thread() {
public void run() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String userid = settings.getString("userid", "uid");
Log.d("DataHandlerService", userid);
// Download-URL
String URL = "http://api.i-v-o.ch/users/" + userid
+ "/assignments.json";
Log.d("Request-URL", URL);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response;
try {
HttpGet request = new HttpGet();
request.setURI(new URI(URL));
request.addHeader("Content-Type",
"application/x-www-form-urlencoded");
response = client.execute(request);
int statuscode = response.getStatusLine().getStatusCode();
switch (statuscode) {
case 200:
if (response != null) {
StringBuilder sb = new StringBuilder();
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity()
.getContent()));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line + "\n");
}
String result;
result = sb.toString();
Log.d("Response", result);
JSONReader(result); //here the json will be generated
}
break;
case 500:
// Error-Handling
break;
}
} catch (Exception e) {
e.printStackTrace();
Log.e("DataHandler", "URLConnection-Error" + e);
}
}
};
t.start();
}
Here's the Response you asked for, like you see a part of it isn't there!:
[{"created_at":"2012-01-06T17:10:00Z","end_datetime":"2008-03-25T13:00:00Z","id":2127,"start_datetime":"2008-03-25T13:00:00Z","updated_at":"2012-01-06T17:10:00Z","title":"2127 Foobar","referee_forename":"Peter","referee_surname":"Gertsch","referee_full_name":"Peter Gertsch","category_title":"Installation - SAT","status_title":"Closed - Erfolgreich"},{"created_at":"2012-01-06T17:10:03Z","end_datetime":"2008-04-04T12:00:00Z","id":2134,"start_datetime":"2008-04-04T12:00:00Z","updated_at":"2012-01-06T17:10:03Z","title":"2134 Foobar","referee_forename":"Daniel","referee_surname":"Brunner","referee_full_name":"Daniel Brunner","category_title":"Installation - SAT","status_title":"Closed - Erfolgreich"},{"created_at":"2012-01-06T17:10:03Z","end_datetime":"2008-04-07T12:00:00Z","id":2136,"start_datetime":"2008-04-07T12:00:00Z","updated_at":"2012-01-06T17:10:03Z","title":"2136 Foobar","referee_forename":"Andreas","referee_surname":"Lutz","referee_full_name":"Andreas Lutz","category_title":"Installation - SAT","status_title":"Closed - technisches problem"},{"created_at":"2012-01-06T17:10:08Z","end_datetime":"2008-05-22T07:00:00Z","id":2144,"start_datetime":"2008-05-22T07:00:00Z","updated_at":"2012-01-06T17:10:08Z","title":"2144 Foobar","referee_forename":"Pascal","referee_surname":"Pichand","referee_full_name":"Pascal Pichand","category_title":"Installation - SAT","status_title":"Closed - Erfolgreich"},{"created_at":"2012-01-06T17:10:08Z","end_datetime":"2008-05-15T07:00:00Z","id":2145,"start_datetime":"2008-05-15T07:00:00Z","updated_at":"2012-01-06T17:10:08Z","title":"2145 Foobar","referee_forename":"Hansruedi","referee_surname":"W\u00fcrgler","referee_full_name":"Hansruedi W\u00fcrgler","category_title":"Installation - SAT","status_title":"Closed - Erfolgreich"},{"created_at":"2012-01-06T17:10:08Z","end_datetime":"2008-05-26T08:00:00Z","id":2146,"start_datetime":"2008-05-26T08:00:00Z","updated_at":"2012-01-06T17:10:08Z","title":"2146 Foobar","referee_forename":"Martina","referee_surname":"Issler","referee_full_name":"Martina Issler","category_title":"Installation - SAT","status_title":"Closed - Erfolgreich"},{"created_at":"2012-01-06T17:10:08Z","end_datetime":"2008-06-03T14:00:00Z","id":2147,"start_datetime":"2008-06-03T14:00:00Z","updated_at":"2012-01-06T17:10:08Z","title":"2147 Foobar","referee_forename":"Matthias ","referee_surname":"Kuhn","referee_full_name":"Matthias Kuhn","category_title":"Installation - SAT","status_title":"Closed - Erfolgreich"},{"created_at":"2012-01-06T17:10:12Z","end_datetime":"2008-07-07T07:00:00Z","id":2157,"start_datetime":"2008-07-07T07:00:00Z","updated_at":"2012-01-06T17:10:12Z","title":"2157 Foobar","referee_forename":"Eberhard","referee_surname":"Polatzek","referee_full_name":"Eberhard Polatzek","category_title":"Installation - SAT","status_title":"Closed - Erfolgreich"},{"created_at":"2012-01-06T17:10:13Z","end_datetime":"2008-07-11T08:00:00Z","id":2161,"start_datetime":"2008-07-11T08:00:00Z","updated_at":"2012-01-06T17:10:13Z","title":"2161 Foobar","referee_forename":"Magali","referee_surname":"Bohin","referee_full_name":"Magali Bohin","category_title":"Installation - SAT","status_title":"Closed - Erfolgreich"},{"created_at":"2012-01-06T17:10:14Z","end_datetime":"2008-07-25T08:30:00Z","id":2163,"start_datetime":"2008-07-25T08:30:00Z","updated_at":"2012-01-06T17:10:14Z","title":"2163 Foobar","referee_forename":"(Hotel Centrum Griesalp)","referee_surname":"Haltenegg Betriebs AG","referee_full_name":"(Hotel Centrum Griesalp) Haltenegg Betriebs AG","category_title":"Installation - SAT","status_title":"Closed - Erfolgreich"},{"created_at":"2012-01-06T17:10:16Z","end_datetime":"2008-08-07T09:00:00Z","id":2170,"start_datetime":"2008-08-07T09:00:00Z","updated_at":"2012-01-06T17:10:16Z","title":"2170 Foobar","referee_forename":".","referee_surname":"SAC Hollandiah\u00fctte","referee_full_name":". SAC Hollandiah\u00fctte","category_title":"Installation - SAT","status_title":"Closed - Erfolgreich"},{"created_at":"2012-01-06T17:10:16Z","end_datetime":"2009-05-07T06:30:00Z","i
Ah. Right, the problem isn't your connection or anything like that. Your service is returning an array - not an object - thus you should parse it like this:
HttpResponse response = ...
if (.. validate status ..) {
JSONArray array = new JSONArray(HttpEntityUtils.toString(response.getEntity()));
// Your JSONArray is now ready to play with.
}
And consider using an AsyncTask instead of a Thread, like this:
class AssignmentsTask extends AsyncTask<String, Void, JSONArray> {
@Override
protected JSONArray doInBackground(String... params) {
final String url = "http://api.i-v-o.ch/users/" + params[0]
+ "/assignments.json";
try {
HttpResponse response = mClient.execute(new HttpGet(url));
if (response.getStatusLine().getStatusCode() == 200) {
return new JSONArray(EntityUtils.toString(response.getEntity()));
} else {
Log.w(TAG, "Error receiving assignments for " + params[0] + ", " + response.getStatusLine());
}
} catch (ClientProtocolException e) {
Log.w(TAG, "Proto: Error fetching assignments for " + params[0], e);
} catch (IOException e) {
e.printStackTrace();
Log.w(TAG, "IO: Error reading assignments for " + params[0], e);
} catch (ParseException e) {
Log.w(TAG, "Parse: Error parsing assignments for " + params[0], e);
} catch (JSONException e) {
Log.w(TAG, "JSON: Error parsing JSON for " + params[0], e);
}
return null;
}
@Override
protected void onPostExecute(JSONArray result) {
// Stuff that handles the resulting JSONObject on
// the UI-thread goes here (i.e. update View:s)
// result is null if the operation failed
}
}
And to retrieve an order for the user "116":
new AssignmentsTask().execute("116");
这篇关于DefaultHttpClient改变响应大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!