Hello Frnds This is the code which is i am using with Xcode for iPhone and getting the response correctly, But the issue is this while i am using the same url and the same json for android development then i am getting:-
{"code":"400","message":"Failed loading JSON. Special characters must not be included in the request. Please check the requested JSON."}
I think here is any minor technical issue which i am not finding. I am new in android development i try it 1000 times from last 15 days and getting frustrate. :-(:-(:-(:-(:-(:-(:-(:-(:-(:-( Can anyone provide me the complete solution with complete code for android. I will be highly obliged you.
The code for iPhone is:
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];
After Writing this code i ma getting the perfect response from the server. But how can i get the same response from the server for android development. Please provide me complete solution with complete code of 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.
This is android Code:
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();
}
}
}
}
}
The problem is that you do not use JSONObject listobj
in your request at all, you add s1
to post parameters instead. Use this code:
postParameters.add(new BasicNameValuePair("json_data", listobj.toString()));
Besides, put integer values to JSONObject explicitly: listInvoice.put("page", 1);
instead of listInvoice.put("page", "1");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.