Click here to Skip to main content
15,346,923 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 site but its not working and I don't know why , there is no error? C My site in free hosting

this is my java code ?

public class MainActivity extends AppCompatActivity {
ArrayList<HashMap<String, String>> BrandList;
ListAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
    Toolbar toolbar = (Toolbar) findViewById(;

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

    **new GetBrandList(MainActivity.this,list).execute("http://"my site name!!!"");**


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

    String status= null;
    Activity context;
    ListView listView;

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

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

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

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



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


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

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

            }catch (Exception ex){
            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);
        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();

        return brandList.ToList();



my service1.cs
public interface IService1
    [WebGet(UriTemplate = "GetBrandName")]
    List<BrandName> GetBrandName();
    [WebInvoke(Method = "POST", UriTemplate = "AddBrand", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    int AddBrand(string brand);
public class BrandName
    string brand;
    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!!!"");

What I have tried:

re upload every thing !!! including the site and the datbase
Updated 4-May-21 0:25am
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

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