Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a web service to connect my android app to my vb.net site but its not working and I don't know why , there is no error? C My site in smarterasp.net free hosting

this is my java code ?

public class MainActivity extends AppCompatActivity {
ArrayList<HashMap<String, String>> BrandList;
ListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    BrandList = new ArrayList<HashMap<String, String>>();
    final ListView list = (ListView) findViewById(R.id.list);

    **new GetBrandList(MainActivity.this,list).execute("http://"my site name!!!".etempurl.com/LocalAppTest/service1.svc/GetBrandName");**

}

class GetBrandList extends AsyncTask<String, Void, String> {

    String status= null;
    Activity context;
    ListView listView;

    public GetBrandList(Activity context, ListView listView){
        this.context =context;
        this.listView=listView;
    }
    protected void onPreExecute(){

    }
    protected String doInBackground(String... connUrl){
        HttpURLConnection conn=null;
        BufferedReader reader;

        try{
            final URL url=new URL(connUrl[0]);
            conn=(HttpURLConnection) url.openConnection();
            conn.addRequestProperty("Content-Type", "application/json; charset=utf-8");
            conn.setRequestMethod("GET");
            int result = conn.getResponseCode();
            if(result==200){

                InputStream in=new BufferedInputStream(conn.getInputStream());
                reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder sb=new StringBuilder();
                String line = null;

                while((line=reader.readLine())!=null){
                    status=line;
                }

            }


        }catch(Exception ex){
            ex.printStackTrace();
        }
        return status;
    }
    protected void onPostExecute(String result){
        super.onPostExecute(result);

        if(result!=null){
            try{

                ArrayList<String> stringArrayList = new ArrayList<String>();
                JSONArray jsonArray = new JSONArray(result);
                for(int i=0; i<jsonArray.length(); i++){
                    JSONObject object = jsonArray.getJSONObject(i);
                    String Brand= object.getString("Brand");

                    HashMap<String, String> itemList = new HashMap<String, String>();
                    itemList.put("Brand",Brand);

                    BrandList.add(itemList);
                }
                adapter = new SimpleAdapter(MainActivity.this, BrandList,R.layout.brandlist, new String[]{"Brand"},new int[]{R.id.txtBrand});
                ((AdapterView<ListAdapter>) listView).setAdapter(adapter);


            }catch (Exception ex){
                ex.printStackTrace();
            }
        }else{
            Toast.makeText(MainActivity.this,"Could not get any data.", Toast.LENGTH_LONG).show();
        }
    }
}

this is my service1.svc.cs :
public class Service1 : IService1
{
    private string conStr = "Data Source="my connection string!!!";";
    public List<BrandName> GetBrandName()
    {
        List<BrandName> brandList = new List<BrandName>();
        SqlConnection connection = new SqlConnection(this.conStr);
        connection.Open();
        SqlCommand cmd = new SqlCommand("SELECT * from Password", connection);
        cmd.CommandType = CommandType.Text;
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            BrandName brand = new BrandName();
            brand.Brand = sdr["BrandName"].ToString();
            brandList.Add(brand);

        }
        return brandList.ToList();
    }

    }

}

my service1.cs
public interface IService1
{
    [OperationContract]
    [WebGet(UriTemplate = "GetBrandName")]
    List<BrandName> GetBrandName();
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "AddBrand", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    int AddBrand(string brand);
}
[DataContract]
public class BrandName
{
    string brand;
    [DataMember]
    public string Brand
    {
        get { return brand; }
        set { brand = value; }
    }
}

is this code right "the service url" :
new GetBrandList(MainActivity.this,list).execute("http://"my site name!!!".etempurl.com/LocalAppTest/service1.svc/GetBrandName");


What I have tried:

re upload every thing !!! including the site and the datbase
Posted
Updated 4-May-21 0:25am
Comments
Mike V Baker 13-Jul-18 9:12am    
Have you tried typing the URL into a browser? When I do that with my services I get a json file with the contents that the app will get. (assuming it's returning json)

How long have you waited for the response from the server when you're running the Android app? In my case the timeout value is something very long.

OH! What about this in your SqlCommand? "SELECT * from Password"

Is that a typo from transferring the code to the question or is that the way it is in your service?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900