我有一种令人讨厌的情况,因为我无法得到结果。 我试图使用localhost JSON GET调用。当我在浏览器中使用此URL时,我得到一个JSON RESULT(不是xml) http://localhost:8080/Boxbackend/webresources/entities.movingboxes
[
{
"boxId": 1,
"boxName": "Stuff_1"
},
{
"boxId": 2,
"boxName": "Stuff_2"
},
{
"boxId": 3,
"boxName": "Stuff_3"
}
]
我使用这个netbeans教程来创建RESTful Web服务
https://netbeans.org/kb/docs/websvc/rest.html
我将这个例子用于android studio
https://www.learn2crack.com/2013/10/android-asynctask-json-parsing-example.html
当网址传递时:
JSONObject json = jParser.getJSONFromUrl(url);
json获取结果null并触发异常 有时例外:android.os.NetworkOnMainThreadException
而不是localhost我有时也使用10.0.2.2
我有点沮丧..
也许有人可以提供帮助额外信息:
Mainactivity onCreate有一个按钮
Button box_list_button=(Button)findViewById(R.id.box_list_button);
box_list_button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
i = new Intent(getApplicationContext(), boxlist.class);
startActivity(i);
}
});
boxlist.class就像
public class boxlist extends Activity {
ArrayList mobileArray;
String [] mobileResult = {"there is no data"};
private static String url = "http://localhost:8080/Boxbackend/webresources/entities.movingboxes";
JSONArray mbox = null;
private static final String TAG_BNAME = "boxName";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movingbox_layout);
new JSONParse().execute();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
// uid = (TextView)findViewById(R.id.uid);
// name1 = (TextView)findViewById(R.id.name);
// email1 = (TextView)findViewById(R.id.email);
pDialog = new ProgressDialog(boxlist.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
} .......