gpt4 book ai didi

android - 使用 Android 从 URL 获取 JSON 数据?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:30:04 26 4
gpt4 key购买 nike

我正在尝试通过使用用户名和密码解析登录 URL 来获取 JSON 数据。我试过使用下面的代码,但我无法得到任何回应。请帮助我。

我正在使用 HTTP 进程和 API 级别 23。

我需要解析我的 URL 并获得下面的响应

{
"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);
}

}

}

最佳答案

特别是 Android SDK 23 获取 JSON 的简单方法:

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+"\n");
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 - 使用 Android 从 URL 获取 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33229869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com