gpt4 book ai didi

java - 用户信息Json数据只有在登录后才会出现,如何在android中解析该Json数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:06 24 4
gpt4 key购买 nike

我正在开发一个应用程序,其中我有两个 JSon url 1.(例如www.xxxxxxx.com)登录应用程序(通过提供用户名和密码)并2.(例如www.xxxxxx xxxx xxx.com)获取登录用户信息,但是我们只能在登录后获取用户信息,登录之前第二个url不显示任何内容..那么如何从android中的第一个json数据解析第二个json数据...这是我的 Login.java(登录的第一个 json url)

public class Login extends Activity {
private static final String SERVICE_URI = "http://xxxxxxxxxxxxxxxxxxxx.com/login";
private static Context mContext;
public EditText edittext_username, edittext_password;
Button button_submit,reg;
public static final String TAG = "MYTAG";
Person person;
static TextView txt_Error;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mContext = this;
edittext_password = (EditText) findViewById(R.id.textEmailAddress);
edittext_username = (EditText) findViewById(R.id.txtUsername);
button_submit = (Button) findViewById(R.id.buttonLogin);
txt_Error =(TextView)findViewById(R.id.txtError);
button_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
new MyAsyncTask().execute();
}
});
}
public String POST(String url, Person person){
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("username", person.getName());
jsonObject.accumulate("password", person.getCountry());
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(httpResponse.getStatusLine().getStatusCode()==200){
Intent login = new Intent(mContext, MainActivity.class);
mContext.startActivity(login);
}else{
txt_Error.setText("Sorry!! Incorrect Username or Password");
}
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private class MyAsyncTask extends AsyncTask<Void, Void, String> {
ProgressDialog mProgressDialog;
private DefaultHttpClient httpclient;
private String username, password;
private HttpPost httppost;
private ArrayList<NameValuePair> nameValuePairs;
private HttpResponse response;
private HttpEntity entity;
@Override
protected void onPostExecute(String result) {
mProgressDialog.dismiss();
}
@Override
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(Login.this, "", "Loading...");
}
@Override
protected String doInBackground(Void... params) {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://xxxxxxxxxxxxxxxxxxxxxxx.com/login");
person = new Person();
person.setName(edittext_username.getText().toString());
person.setCountry(edittext_password.getText().toString());
return POST(SERVICE_URI, person);
}
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}

}

最佳答案

有很多方法可以将 JSON 字符串解析为对象表示。

我看到您已经使用 JSONObject创建 POST 请求的正文。在使用 convertInputStreamToString 将响应正文转换为字符串后,您可以使用 new JSONObject(jsonString) 从响应正文中创建一个对象。

但是,有更好的替代方案可以将 JSON 映射到您自己的对象模型,例如Gson 库。

看看这个答案 https://stackoverflow.com/a/31743324/2914666或侧面列出的任何相关问题。

关于java - 用户信息Json数据只有在登录后才会出现,如何在android中解析该Json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35916009/

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