0

I am trying to access a HTTPS request, but When I am trying to access it from my Android app it does not reach to server but the same url is working fine from chrome http client.

Here is my code for POST HTTP request

 public String POST(String url){
    InputStream inputStream = null;
    String result = "";
    try {
        String jsonreply;
        StringBuilder builder = new StringBuilder();
        HttpClient client = getNewHttpClient();
        HttpPost httpPost = new HttpPost(url);
        Log.d(TAG, "URLS"+url);
        try {
            HttpResponse response = client.execute(httpPost);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            } else {
                Log.d(TAG, "Status code"+statusCode);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();

        } catch (IOException e) {
             e.printStackTrace();
        }
        jsonreply = builder.toString();
        return jsonreply;

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    // 11. return result
    return result;
}

the code is always is returning statusCode 404(Not Found). I am very sure that url is correct as I am accessing same url in my other apps too. I can not provide the original url but the fake but similar is this one

https://test.test.se/user_session.json?email=test.test%40gmail.se&password=>Test12345%21%40%23%24&brand=LGE&phonemodel=Nexus+5&os_version=4.4.4&os_type=Android

Thanks for help

user2323
  • 49
  • 2
  • 11

2 Answers2

0

Pass your url to this Function..

public static String readFeed(String URL) {

    StringBuilder stringBuilder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(URL);
    try {
            HttpResponse response = client.execute(httpGet);
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) 
                {
                    stringBuilder.append(line);
                }

    } catch (ClientProtocolException e) 
    {
        e.printStackTrace();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
return stringBuilder.toString();
}

The Result is return in String format.

Harshal
  • 126
  • 7
0

use this one.. try { HttpClient httpclient = new DefaultHttpClient();

            HttpContext localcon=new BasicHttpContext();
            HttpGet httpget=new HttpGet(url1);

            HttpResponse response=httpclient.execute(httpget,localcon);
            } catch (ClientProtocolException e) {

                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
Harshal
  • 126
  • 7