0

通常の json リンクからデータを取得できますが、パスワードで保護されたリンクが 1 つあります。

これを 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 に答える