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