我将URL从一个活动发送到另一个活动,如下所示:
startActivity(new Intent(MainActivity.this, SecondActivity.class).putExtra("key", fullurl));
得到这样的话:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
url = getIntent().getStringExtra("key");
}
如何把它放在这里?
jsonobject = JSONfunctions.getJSONfromURL(url);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
url = getIntent().getStringExtra("key");
TextView txt = (TextView) findViewById(R.id.textView2);
txt.setText(url);
new DownloadJSON().execute();
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
world = new ArrayList<>();
jsonobject = JSONfunctions.getJSONfromURL(url);
try {
// Some code
}
}
}
答案 0 :(得分:0)
你可以做这样的事情
//发出网址请求并获得回复 String jsonStr = sh.makeServiceCall(url,ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone node is JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_EMAIL, email);
contact.put(TAG_PHONE_MOBILE, mobile);
// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
答案 1 :(得分:0)
假设您的网址是一个字符串,并且您想将其转换为网址对象。
URL mUrl = new URL(url);
URI uri = new URI(mUrl.getProtocol(), mUrl.getUserInfo(), mUrl.getHost(), mUrl.getPort(), mUrl.getPath(), mUrl.getQuery(), mUrl.getRef());
mUrl = uri.toURL();
在此之后你可以使用JSONfunctions .getJSONfromURL(mUrl);
答案 2 :(得分:0)
第1步
更改
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
到
private class DownloadJSON extends AsyncTask<URL, Void, Void> {
@Override
protected Void doInBackground((URL... url) {
第2步
更改
new DownloadJSON().execute();
到
try{
URL myUrl = new URL(url);
new DownloadJSON().execute(myUrl);
}
catch(MalformedURLException mue){
Log.e("Invalid url","Invalid url");
}
第3步
更改
jsonobject = JSONfunctions.getJSONfromURL(url);
到
jsonobject = JSONfunctions.getJSONfromURL(url[0]);