如何从json链接中检索数据?

时间:2012-07-05 06:20:19

标签: android json connection

我可以从普通的json链接中检索数据但是我有一个受密码保护的链接。

比如何将其与我的Android应用程序连接?

请帮助我? 我正在使用的连接正常链接的代码就在这里。

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;
}

2 个答案:

答案 0 :(得分:0)

尝试将此添加到您的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));

答案 1 :(得分:0)

    ArrayList<String> fetchsosfromID = new ArrayList<String>();
    String result = "";
    InputStream is = null;

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("username", "%@"));
    nameValuePairs.add(new BasicNameValuePair("password", "%@"));

    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials("<username>", "<password>"));

        HttpPost httppost = new HttpPost(
                "http://handbags4women.com/pioneer-iphone/login.php?");
        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 {

        JSONObject json_data = new JSONObject(result);

        Log.v("log_tag", "daily_data " + fetchsosfromID);

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
相关问题