[英]Parsing JSON Issue for Android
你好,Frnds 这是我在 iPhone 上使用 Xcode 并正确获得响应的代码,但问题是,当我使用相同的 url 和相同的 json 进行 android 开发时,我得到:-
{"code":"400","message":"Failed loading JSON. Special characters must not be included in the request. Please check the requested JSON."}
我认为这是我没有发现的任何小技术问题。 我是 android 开发的新手,在过去的 15 天里我尝试了 1000 次并且感到沮丧。 :-(:-(:-(:-(:-(:-(:-(:-(:-(:-( 谁能给我提供完整的解决方案和 android 的完整代码。我将非常感谢你。
iPhone 的代码是:
NSURL *url = [NSURL URLWithString:@"http://www.invoicera.com/app/api/check_json_api.php?token=7B92C122473A3D6F54E60D20AC5526D0"];
NSString *jsonRequest = [NSString stringWithFormat:@"&json_data=%@",[[NSString stringWithFormat:@"{\"listInvoice\":{\"client_id\":\"\",\"date_from\":\"\",\"date_to\":\"\",\"invoice_number\":\"\",\"invoice_record_status\":\"\",\"invoice_status\":\"\",\"page\":\"1\",\"per_page_record\":\"20\"}}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%@",jsonRequest);
NSData *json_data = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
NSLog(@"%@",json_data);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: json_data];
NSLog(@"%@",json_data);
// [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [json_data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
dataUsingEncoding:NSUTF8StringEncoding
allowLossyConversion:YES]];
// [NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]
initWithRequest:request
delegate:self];
// Successful connection.
if (nsUrlConnection) {
// [self initSpinner];
// [self spinBegin];
NSMutableData *data = [[NSMutableData alloc] init];
self.receivedData=data;
[data release];
}
// Unsuccessful connection.
else {
}
// Clean up
[url release];
[request release];
编写此代码后,我可以从服务器获得完美的响应。 但是对于 android 开发,我怎样才能从服务器获得相同的响应。 请为我提供完整的解决方案,完整代码为 android。
{"@attributes":{"status":"200"},"invoices":{"@attributes":{"page":"1","per_page_record":"20","total_pages":"13","total_records":"246"},"invoice":[{"client":{"client_id":"421","organization":"max styles","address":"H-189,Vasundhara","billing_address":{"street":"H-189,Vasundhara","city":{},"state":{},"zip":........................etc etc etc.
这是 android 代码:
public class JSONParser {
String s1 = "{\"listInvoice\":{\"client_id\":\"\",\"date_from\":\"\",\"date_to\":\"\",\"invoice_number\":\"\",\"invoice_record_status\":\"\",\"invoice_status\":\"\",\"page\":\"1\",\"per_page_record\":\"20\"}}";
public String payload;
public JSONParser(String[]array) {
this.payload = null;
}
public void excuteHttpPost() throws Exception {
BufferedReader in = null;
try {
// Creating HTTP client
JSONObject listobj = new JSONObject ();
JSONObject listInvoice = new JSONObject ();
listInvoice.put("client_id","");
listInvoice.put("date_from","");
listInvoice.put("date_to","");
listInvoice.put("invoice_number","");
listInvoice.put("invoice_record_status","");
listInvoice.put("invoice_status","");
listInvoice.put("page","1");
listInvoice.put("per_page_record","10");
listobj.put("listInvoice", listInvoice);
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(ConstantList.invoicelistURL);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>(1);
postParameters.add(new BasicNameValuePair("json_data", s1));
Log.d("Http Response:", postParameters.toString());
// Build JSON string
request.setHeader("Content-type", "application/x-www-form-urlencoded");
request.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = client.execute(request);
// writing response to log
Log.d("Http Response:", response.toString());
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line+NL);
}
in.close();
payload = sb.toString();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
问题是您根本不在请求中使用JSONObject listobj
,而是将s1
添加到 post 参数。 使用此代码:
postParameters.add(new BasicNameValuePair("json_data", listobj.toString()));
此外,将 integer 值明确地放入 JSONObject: listInvoice.put("page", 1);
而不是listInvoice.put("page", "1");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.