android - 如何在 android 中使用身份验证连接到 Web 服务 JSON?

标签 android json web-services

我对此有很大的疑问。我的 JSON 有这个 url http://phsjulchsvr4.therock.com.ph:1217/api/users我将使用这段代码解析它:

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;

    }
}

问题是我无法连接到这个 url,因为它有一个身份验证。我该怎么办。要使用我的 JSONParser 类连接此 url? HttpResponse 可以执行url

最佳答案

以下是您可能需要的:

public String getValueResult(String url) {  
    System.out.println(url);

    String username = "admin";
    String password = "admin";

    String unp = username+":"+password;

    try {           
        HttpClient httpclient = new DefaultHttpClient();
        // HttpPost httppost = new HttpPost(domain.trim()+"/qhms/wse_hmlogin.php?loginid="+user.trim()+"&logpass="+pass.trim());
        // HttpPost httppost = new HttpPost(domain+"/qhms/wse_hmlogin.php?loginid="+user+"&logpass="+pass);
        // HttpPost httppost = new HttpPost("http://svv.in.net/service/index.php?user=Customer&pass=cus&des=rtr%20r%20rtr%20trtrt");
        HttpPost httppost = new HttpPost(url);
        // httppost.setHeader( "Authorization","Basic "+"admin:admin");
        String encoded_login = Base64.encodeToString(unp.getBytes(), Base64.NO_WRAP);
        httppost.setHeader(new BasicHeader("Authorization", "Basic "+encoded_login));

        System.out.println("URL="+httppost.toString());

        // HttpPost httppost = new HttpPost("http://quantumr.info/qrms/call_service/wse_getitem.php?compkey=astres1312");
        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);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");

        String line = "0";
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }
    return result;
}

关于android - 如何在 android 中使用身份验证连接到 Web 服务 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17782597/

相关文章:

json - 如何配置 Circe 以停止使用嵌套类名作为编码 JSON 中的键名?

wcf - 提高 WCF 性能

c# - C# 动态加载数据表到 JS 数组

android - SD卡卸载android时获取广播接收

Android:WebView,如何全屏并隐藏标题

android - 删除索引无法正常工作的SQlite Android

html - 如何为网站制作程序?我应该知道什么类型的技能/代码?

android - 使用 okhttp 拦截器检查网络连通性

javascript - JSON 中的函数

jquery - 如何从 JSON 响应中获取值