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

标签 java android json import

我正在开发一个应用程序,其中我有两个 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/

相关文章:

mysql - 查找 MySQL JSON 对象或数组的交集

java - 用于扫描的简单 try & catch 代码不适用于十进制值

java - 为什么 TabLayout 总是打乱 Tab 键顺序?

java - 从 Json 数组中的 Json 数组获取数据 Android Java

java - 即使使用异步也会出现 networkOnMainThreadException 错误

android - 包含在内容布局中时不显示 LinearLayout

jquery - 添加类并动态更改 css (jquery)

java - 如何从 servlet 上的列表中打开文件

java - 自动实体映射类似于 JSF 的 O/R 映射?

Java 注释约定