mardi 5 mai 2015

Android Studio: How to propperly consume remote https WCF service?

Im trying to get data from a WCF remote webservice using Android Studio, I followed a tutorial (Links down there!), wich works locally with a windows IIS hosted WCF webservice.

Now Im trying to edit the code for my purpouses and Im stuck again.

I have some questions about:

  1. How to request data to a remote WCF server by android studio?

    I need to make a get request by json like this:

    { "Status":"String", "City":"String", "Address":"String", "State":"String", "Id":"Integer" }

  2. How to access https instead http? How to send and receive data throgh WCF webserver remotely ,(https)

Here is the code I have for android, note that all the show is around the class HttpClientHelper, please check the code below (Whole code on links):

-Server source code and tutorial(cs )

http://ift.tt/1bnJ9k4

http://ift.tt/1KfsoDr

-Android source code and tutorial

http://ift.tt/1ONxe1i

http://ift.tt/1Kfsn2x

The proyect map:

AndroidClientActivity Customer HttpClientHelper <---- This is the class below ObjectAdapter

The code:

package packages.AndroidClient;

import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class HttpClientHelper {

    //Tag para identificar en el Log
    private static final String TAG = "HttpClientHelper";


    public static JSONArray GET(String OperationName) throws Exception
    {
        BufferedReader reader =null;
        StringBuilder sb =null;
        try 
        {
        DefaultHttpClient httpClient = new DefaultHttpClient();


 /*
 * I GUESS I  NEED TO MAKE A JSON REQUEST AROUND HERE AND I DONT KNOW HOW TO DO IT

 I need to make a get request by json like  this:
   {
   "Status":"String",
    "City":"String",
    "Address":"String",
    "State":"String",
    "Id":"Integer",
}
 *
 *
 *
 *
  */


 /*   The line below works perfect locally but i need to work with it remotely and send request data as well, and work with https
     *   HttpGet request = new HttpGet("http://ift.tt/1bwvPtH" + OperationName);
     */


           HttpGet request = new HttpGet("http://ift.tt/1dLNaAA");


            HttpResponse response = httpClient.execute(request);
        HttpEntity responseEntity = response.getEntity();          

        InputStream stream = responseEntity.getContent();    

        reader = new BufferedReader(new InputStreamReader(stream));
        sb = new StringBuilder();

            String line = reader.readLine();

            while (line != null) 
            {
                sb.append(line);
                line = reader.readLine();
            }
        }
        catch (Exception ex) 
        {
            //Procesamos el error
            Log.e(TAG, "Error al crear la actividad. Error: " + ex.getMessage() );
            throw ex;
        }   
        finally 
        {
            reader.close();
        }
        return new JSONArray(sb.toString());        

    }
}

The links of the original example below

NOTE: To make it run switch off all firewalls.

Aucun commentaire:

Enregistrer un commentaire