您好 Frnds 这是我与 Xcode for iPhone 一起使用并正确获得响应的代码,但问题是当我使用相同的 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.
这是安卓代码:
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();
}
}
}
}
}