你好,先生,我想从我的android项目中读取Web服务的数据 [英] hello ,sir,i wanna to read data from web service from my android project
问题描述
我的代码如下;
私有类DownloadJSON扩展了AsyncTask< void,> {$ / $
@Override
protected void onPreExecute(){
super.onPreExecute();
//创建progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
//设置progressdialog标题
mProgressDialog .setTitle(Android JSON Parse Tutorial);
//设置progressdialog消息
mProgressDialog.setMessage(Loading ...);
mProgressDialog.setIndeterminate(false);
//显示progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void ... params){
//创建数组
arraylist = new ArrayList< hashmap>< string,>>();
//从给定的URL地址检索JSON对象
jsonobject = JSONfunctions
.getJSONfromURL(http://192.168.0.77:8083/MyService.svc/ForAllProduct);
试试{
//在JSON中找到数组名称
jsonarray = jsonobject.getJSONArray(null);
for(int i = 0;我< jsonarray.length(); i ++){
HashMap< string,> map = new HashMap< string,>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put(Code,jsonobject.getString(Code));
map.put(Name,jsonobject.getString(Name));
map.put(Size,jsonobject.getString(Size));
map.put(Color,jsonobject.getString(Color));
map.put(Category,jsonobject.getString(Category));
//将JSON对象设置为数组
arraylist.add (地图);
}
} catch(JSONException e){
Log.e(Error,e.getMessage()) ;
e.printStackTrace();
}
返回null;
}
@Override
protected void onPostExecute (Void args){
//在listview_main.xml中找到listview
listview =(ListView)findViewById(R.id.listview);
//将结果传递给ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this,arraylist);
//将适配器设置为ListView
listview.setAdapter(适配器);
//关闭progressdialog
mProgressDialog.dismiss();
}
}
========================
但发生错误在doInBackground()并且没有连接到我的web服务api。
my code are as following ;
private class DownloadJSON extends AsyncTask<void,> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Android JSON Parse Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<hashmap><string,>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://192.168.0.77:8083/MyService.svc/ForAllProduct");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray(null);
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<string,> map = new HashMap<string,>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("Code", jsonobject.getString("Code"));
map.put("Name", jsonobject.getString("Name"));
map.put("Size", jsonobject.getString("Size"));
map.put("Color", jsonobject.getString("Color"));
map.put("Category",jsonobject.getString("Category"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
========================
but error occur at doInBackground() and not connect to my web service api.
推荐答案
我认为问题来自你的链接
http://192.168.0.77:8083/MyService.svc/ForAllProduct [ ^ ]
ForAllProduct是什么这个文件的扩展名。这是ForAllProduc.php还是.asp
如果你发布错误信息会更好
I think the problem come from your link
http://192.168.0.77:8083/MyService.svc/ForAllProduct[^]
ForAllProduct what's is the extension of this file. Is this ForAllProduc.php or .asp
It's better if you post the error message
这篇关于你好,先生,我想从我的android项目中读取Web服务的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!