2012-07-05 181 views
0

我可以从普通的json链接检索数据但我有一个链接,它是密码保护。如何从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; 
} 
+0

我觉得*密码*和*用户名*应POST参数,所以你应该通过通过'nameValuePairs'这些值。 – adatapost

+0

我没有得到您的问题。您在解析JSON数据或从需要某种身份验证的服务器检索JSON时遇到问题? – AndoAiron

+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)); 
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()); 
    }