0

我可以从普通的 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;
}
4

2 回答 2

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));
于 2012-07-05T06:46:45.617 回答
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());
    }
于 2012-07-05T09:32:40.083 回答