我正在开发一个应用程序,其中我有两个JSon url 1.(例如www.xxxxxx.com.com)到登录应用程序(通过提供用户名和密码)和 2.(例如www.xxxxxx xxxx xxx.com)登录用户信息,但我们只能在登录后获取用户信息,在登录之前第二个url没有显示任何内容..所以如何从android中的第一个json数据解析第二个json数据... 这是我的Login.java(登录的第一个json网址)
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; }
}
答案 0 :(得分:0)
有很多方法可以将JSON字符串解析为对象表示。
我看到你已经使用JSONObject来创建POST请求的正文。在您使用new JSONObject(jsonString)
将其转换为字符串后,您可以使用convertInputStreamToString
从响应正文创建对象。
然而,有更好的选择将JSON映射到您自己的对象模型,例如Gson图书馆。
查看此答案https://stackoverflow.com/a/31743324/2914666或旁边列出的任何相关问题。