Java 用户信息Json数据仅在登录后显示,如何在android中解析该Json数据

Java 用户信息Json数据仅在登录后显示,如何在android中解析该Json数据,java,android,json,import,Java,Android,Json,Import,我正在开发一个应用程序,其中有两个JSon url 1.(例如www.xxxxxxx.com)登录应用程序(通过提供用户名和密码)和 2.(例如www.xxxxxx xxxx.com)登录用户信息,但我们只能在登录后、登录前获取用户信息。第二个url不显示任何内容。因此,如何从android中的第一个json数据解析第二个json数据。。。 这是我的Login.java(登录的第一个JSONURL) 公共类登录扩展活动{ 私有静态最终字符串服务_URI=”http://xxxxxxxxxxxxx

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

公共类登录扩展活动{
私有静态最终字符串服务_URI=”http://xxxxxxxxxxxxxxxxxxxx.com/login";
私有静态上下文mContext;
公共EditText EditText\u用户名、EditText\u密码;
按钮提交,注册;
公共静态最终字符串TAG=“MYTAG”;
个人;
静态文本视图txt_错误;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
mContext=这个;
edittext_密码=(edittext)findViewById(R.id.textEmailAddress);
edittext_用户名=(edittext)findViewById(R.id.txtUsername);
按钮提交=(按钮)findViewById(R.id.buttonLogin);
txt_Error=(TextView)findViewById(R.id.txError);
按钮\u submit.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
新建MyAsyncTask().execute();
}
});
}
公共字符串帖子(字符串url,Person){
InputStream InputStream=null;
字符串结果=”;
试试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
字符串json=“”;
JSONObject JSONObject=新的JSONObject();
累积(“用户名”,person.getName());
累积(“密码”,person.getCountry());
json=jsonObject.toString();
StringEntity se=新的StringEntity(json);
httpPost.setEntity(se);
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”、“应用程序/json”);
HttpResponse HttpResponse=httpclient.execute(httpPost);
inputStream=httpResponse.getEntity().getContent();
如果(httpResponse.getStatusLine().getStatusCode()==200){
意图登录=新意图(mContext,MainActivity.class);
mContext.startActivity(登录);
}否则{
txt_Error.setText(“对不起!!用户名或密码不正确”);
}
如果(inputStream!=null)
结果=convertInputStreamToString(inputStream);
其他的
结果=“不起作用!”;
}捕获(例外e){
d(“InputStream”,例如getLocalizedMessage());
}
返回结果;
}
私有类MyAsyncTask扩展了AsyncTask{
进程对话框;
私有默认httpclient httpclient;
私有字符串用户名、密码;
私有HttpPost HttpPost;
私有ArrayList nameValuePairs;
私有HttpResponse响应;
私有http实体;
@凌驾
受保护的void onPostExecute(字符串结果){
mProgressDialog.disclose();
}
@凌驾
受保护的void onPreExecute(){
mProgressDialog=ProgressDialog.show(Login.this,“,”加载…);
}
@凌驾
受保护字符串doInBackground(Void…params){
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://xxxxxxxxxxxxxxxxxxxxxxx.com/login");
person=新的person();
person.setName(edittext_username.getText().toString());
person.setCountry(edittext_password.getText().toString());
回程邮件(服务人员,个人);
}
}
私有静态字符串convertInputStreamToString(InputStream InputStream)引发IOException{
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream));
字符串行=”;
字符串结果=”;
而((line=bufferedReader.readLine())!=null)
结果+=行;
inputStream.close();
返回结果;
}

}

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

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

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


请查看此答案或旁边列出的任何相关问题。

有许多方法可以将JSON字符串解析为对象表示形式

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

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


查看此答案或旁边列出的任何相关问题。

在onPostExecute中,您将返回登录响应,从这里可以解析响应并移动到第二个JSON。请用简单的示例告诉我如何解析响应并移动到post execute中的第二个JSON…因为如果我在post execute中解析,我不会从第二个url获得任何数据…请用简单的示例帮助我…请任何人回答我的问题…在ONPOST execute中,你将返回您的登录响应,您可以从中解析响应并移动到第二个JSON。请用简单的示例告诉我如何解析响应并移动到post execute中的第二个JSON…因为如果我在post execute中解析,我不会从第二个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;
    }