0
votes

I have an android app that invoke some php script on a server, and receive json message as response by using a "normal" HTTP connection like:

static JSONObject jObj = null;
static String json = "";

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

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();
jObj = new JSONObject(json);
//code for parse jObj Object

Now i need to transfer entire server on a new server that use ssl for crypt data. Before start server migration, i need to know ho to modify my android source for use it over https. Just only need to modify script address with https (and HttpPost automatically manage all https phases), or i need to do other substantial modification?

2
you only need to change the url to https:// - p0rter
For me it works with https:// in the url - Boe-Dev
so HttpPost automatically negotiate keys and choose crypt algorithm? - giozh

2 Answers

0
votes

Use following code to send json data to server using post method.

try {
            HttpClient httpClient = new DefaultHttpClient();
            JSONObject object = new JSONObject();
            object.put("Username", "testUser@123");
            object.put("Password", "testPassword@123");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("Authentication", object);
            jsonObject.put("RequestType", 4);
            HttpPost postMethod = new HttpPost("your_url");
            postMethod.setEntity(new StringEntity(jsonObject.toString()));
            postMethod.setHeader("Accept", "application/json");
            postMethod.setHeader("Content-type", "application/json");
            HttpResponse response = httpClient.execute(postMethod);
            HttpEntity entity = response.getEntity();
            String response_value = EntityUtils.toString(entity).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
0
votes

You must try this if you are getting response from server.

static JSONObject jObj = null;
        static String json = "";
        StringBuilder builder =new StringBuilder();
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        //httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
         while(is.read()!=-1){
             builder.append((char)is.read());
         }

        is.close();
        json = builder.toString();
        jObj = new JSONObject(json);