简体   繁体   English

如何在Android中通过身份验证连接到Web服务JSON?

[英]How to connect to a web service JSON with authentication in android?

I have a big problem with this. 我有一个大问题。 I have this url for my JSON http://phsjulchsvr4.therock.com.ph:1217/api/users and Im going to parse it using this code: 我的JSON这个网址是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;

    }
}

The problem is I can't connect to this url because it has an authentication. 问题是我无法连接到该URL,因为它具有身份验证。 What should I gonna do. 我该怎么办 To connect this url using my JSONParser class? 要使用我的JSONParser类连接此URL? The HttpResponse can execute the url HttpResponse可以执行url

Try using this when doing the request: 在执行请求时尝试使用此方法:

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        // Set the username and password
        String authStr = "username" + ":" + "password";

        // Encode authentication values, and add to header
        String authStrEncoded = Base64.encode(authStr.getBytes("UTF-8"));
        httpGet.addHeader("Authorization", "Basic " + authStrEncoded);

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

Here is what you might need: 这可能是您需要的:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM