使用 Android 从 URL 获取 JSON 数据? [英] Get JSON Data from URL Using Android?
本文介绍了使用 Android 从 URL 获取 JSON 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过使用用户名和密码解析登录 url 来获取 JSON 数据.我已尝试使用以下代码,但无法得到任何响应.请帮帮我.
I am trying to get JSON data by parsing login url with username and password. I have tried by using below code but I can't get any responses. Please help me.
我使用的是 HTTP 进程和 API 级别 23.
I am using HTTP Process and API level 23.
我需要解析我的 URL 并得到下面的响应
I need to parse my URL and get below Response
{
"response":{
"Team":"A",
"Name":"Sonu",
"Class":"First",
},
"Result":"Good",
}
在我的代码下面:
public class LoginActivity extends Activity {
JSONParser jsonparser = new JSONParser();
TextView tv;
String ab;
JSONObject jobj = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
new retrievedata().execute();
}
class retrievedata extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
jobj = jsonparser.makeHttpRequest("http://myurlhere.com");
// check your log for json response
Log.d("Login attempt", jobj.toString());
try {
ab = jobj.getString("title");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ab;
}
protected void onPostExecute(String ab){
tv.setText(ab);
}
}
}
推荐答案
获取 JSON 的简便方法,尤其适用于 Android SDK 23:
Easy way to get JSON especially for Android SDK 23:
public class MainActivity extends AppCompatActivity {
Button btnHit;
TextView txtJson;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnHit = (Button) findViewById(R.id.btnHit);
txtJson = (TextView) findViewById(R.id.tvJsonItem);
btnHit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new JsonTask().execute("Url address here");
}
});
}
private class JsonTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Please wait");
pd.setCancelable(false);
pd.show();
}
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line+"
");
Log.d("Response: ", "> " + line); //here u ll get whole response...... :-)
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pd.isShowing()){
pd.dismiss();
}
txtJson.setText(result);
}
}
}
这篇关于使用 Android 从 URL 获取 JSON 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文