小编Man*_*ano的帖子

如何从Android中的JSON响应中获取特定价值?

我想"result"从下面的JSON响应中获取值并将其存储在本地.这是代码:

private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            if (jsonStr != null) {
                    try {
                        JSONObject jsonObj = new JSONObject(jsonStr);

                        //JSONArray contacts;
                        contacts = jsonObj.getJSONArray("response");
                        Log.d("Response: ", "> " + contacts);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.e("ServiceHandler", "Couldn't get any data …
Run Code Online (Sandbox Code Playgroud)

android android-json

-2
推荐指数
1
解决办法
5315
查看次数

如何获得服务器响应以及在JSON使用目标c显示警报后服务器响应是否为"成功"

NSString *data = [NSString stringWithFormat:@"username=%@&password=%@",usertxt.text,pwdtxt.text,[NSNumber numberWithBool:YES]];
    NSData *postData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
Run Code Online (Sandbox Code Playgroud)

//预先发送URL请求以发送数据.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSString *url = [NSString stringWithFormat:URL_PATH];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
[request setTimeoutInterval:7.0];

NSURLResponse *_response;
NSError *error;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&_response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
Run Code Online (Sandbox Code Playgroud)

//打印服务器响应

NSLog(@"Login response:%@",str);
Run Code Online (Sandbox Code Playgroud)

//将JSON字符串解析为NSDictionary

 NSDictionary* json = [NSJSONSerialization JSONObjectWithData:urlData
                                                     options:kNilOptions
                                                      error:&error]; 
Run Code Online (Sandbox Code Playgroud)

iphone json objective-c ios

-5
推荐指数
1
解决办法
2298
查看次数

标签 统计

android ×1

android-json ×1

ios ×1

iphone ×1

json ×1

objective-c ×1