1
我有這個大問題。我有這個網址我的JSON http://phsjulchsvr4.therock.com.ph:1217/api/users和我要去使用此代碼來分析它:如何在Android中使用身份驗證連接到Web服務JSON?
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
問題是我無法連接到這個網址,因爲它有一個認證。我該怎麼做。使用我的JSONParser類來連接這個URL? 獲取HttpResponse可以執行URL
的Base64.encode需要的字節[參數的],int不僅是byte [] – NewDroidDev
Ohh對不起,試試這個: String authStrEncoded = new String(Base64.encode(authStr.getBytes(「UTF-8」),Base64.NO_WRAP)); – Henric