如何从JSON链路数据? [英] How to retrieve data from json link?

查看:119
本文介绍了如何从JSON链路数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以检索与正常的JSON链路数据,但我有一个链接,这是密码保护。

比我如何与我的Andr​​oid应用程序连接呢?

请帮帮我吗?
在code为连接到我使用正常的链接在这里。

 的ArrayList<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;();
    nameValuePairs.add(新BasicNameValuePair(track_date,2011-08-09));
    nameValuePairs.add(新BasicNameValuePair(tracker_user_id,+ 374));    尝试{
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httppost =新HttpPost(
                http://abovestress.com/app_stress/fetch_all_detail.php?task=fetchtimefromdateanduserid&);
        httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
        HTT presponse响应= httpclient.execute(httppost);
        HttpEntity实体= response.getEntity();
        是= entity.getContent();
    }赶上(例外五){
        Log.e(log_tag,在HTTP连接错误+ e.toString());
    }
    //响应转换为字符串
    尝试{
        读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
                是,ISO-8859-1),8);
        StringBuilder的SB =新的StringBuilder();
        串线= NULL;
        而((行= reader.readLine())!= NULL){
            sb.append(行+\\ n);
        }
        is.close();
        结果= sb.toString();
        Log.v(log_tag,追加字符串+结果);
    }赶上(例外五){
        Log.e(log_tag,错误转换结果+ e.toString());
    }    //解析JSON数据
    尝试{
        JSONArray jArray =新JSONArray(结果);
        的for(int i = 0; I< jArray.length();我++){
            JSONObject的json_data = jArray.getJSONObject(I)
            fetchsosfromID.add(json_data.getString(track_time));
        }
        Log.v(log_tag,daily_data+ fetchsosfromID);    }赶上(JSONException E){
        Log.e(log_tag,错误分析数据+ e.toString());
    }
}
公共布尔isTimeExistSos(){    返回false;
}


解决方案

尝试将此添加到您的的HttpClient

  //创建用户名和放大器;密码
字符串username =的uname;
字符串密码=passwd文件;
//创建连接客户端
DefaultHttpClient客户端=新DefaultHttpClient(PARAMS);
用于连接客户端//凭证集
client.getCredentialsProvider()
            .setCredentials(
新AuthScope(NULL,-1),新UsernamePasswordCredentials(用户名,密码));

I can retrieve data from normal json link But I have one link which is password protected.

Than How can I connect this with my Android application?

please help me ? The code for connect to normal link I am using is here.

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("track_date", "2011-08-09"));
    nameValuePairs.add(new BasicNameValuePair("tracker_user_id", "" + 374));

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://abovestress.com/app_stress/fetch_all_detail.php?task=fetchtimefromdateanduserid&");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }
    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        Log.v("log_tag", "Append String " + result);
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    // parse json data
    try {
        JSONArray jArray = new JSONArray(result);
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            fetchsosfromID.add(json_data.getString("track_time"));
        }
        Log.v("log_tag", "daily_data " + fetchsosfromID);

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
}
public boolean isTimeExistSos(){

    return false;
}

解决方案

try to add this to your HttpClient:

//create username & password
String username = "uname";
String password = "passwd";
// create connection client
DefaultHttpClient client = new DefaultHttpClient(params);
// set credentials for connection client
client.getCredentialsProvider()
            .setCredentials(
new AuthScope(null, -1), new UsernamePasswordCredentials(username,password));

这篇关于如何从JSON链路数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆