我是Android新手,需要幫助。Json的Android登錄屏幕
我有一個JSON看起來像這樣:
{
"Id": 1,
"Name": "user",
"userId": 4,
"active": true,
"ProfileId": 1,
"Tema": "green",
"Language": "english",
"success": true,
"error": false
}
的Json 2:
{"message":"no user or password","success":false,"erroAplicacao":false}
這是我的代碼:
public class MainActivity extends AppCompatActivity {
EditText usernameWidget;
EditText passwordWidget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
usernameWidget = (EditText) findViewById(R.id.tv_username);
passwordWidget = (EditText) findViewById(R.id.tv_password);
}// END ON CREATE
public class DownloadTask extends AsyncTask<String, Void, String> {
String message = "message";
String loginSuccess;
String Id = "Id";
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
int data = reader.read();
while (data != -1){
char current = (char) data; // each time creates a char current
result += current;
data = reader.read();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
} // END doInBackground
//Method called when the doInBack is complete
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject jsonObject = new JSONObject(result);
Log.i("***JSON ITSELF***", result);
loginSuccess = jsonObject.getString("success");
Log.i("*****LOGIN SUCCESS*****", loginSuccess);
message = jsonObject.getString("message");
Log.i("*****MESSAGE*****", message);
Id = jsonObject.getString("Id");
Log.i("*****ID*****", pessoaFisicaId);
}catch(JSONException e) {
e.printStackTrace();
}// END CATCH
if (loginSuccess.contains("true")){
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}else if (loginSuccess.contains("false")){
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
}// END POST EXECUTE
}// END Download Task
public void login(View view) {
String user = usernameWidget.getText().toString();
String pass = passwordWidget.getText().toString();
String stringJSON = "*URL*login=" + user + "&senha=" + pass;
DownloadTask task = new DownloadTask();
task.execute(stringJSON);
Log.i("*****JSON URL*****", stringJSON);
}// END LOGIN
}// END MAIN
順序如下:
loginSuccess = jsonObject.getString("success");
message = jsonObject.getString("message");
Id = jsonObject.getString("Id");
我從json2(不同的網址)獲取Toast消息(「no user or password」)。
如果我更改順序,可以說,到:
loginSuccess = jsonObject.getString("success");
Id = jsonObject.getString("Id");
message = jsonObject.getString("message");
我不得到一個消息。實際上,我在DownloadTask類的開始處從String消息中獲取了「消息」的值。
看來,json只能得到兩個值,我要求的第一個值。
但有一件事是,只有當用戶或密碼錯誤是,JSON有一個消息(json2):
{"message":"no user or password","success":false,"erroAplicacao":false}
由於我的JSON不能被轉化成一個數組JSON(我試過找來錯誤說這個),我該怎麼辦?
謝謝你,這對我幫助很大,在更好地瞭解JSON。 xD – BlitzkriegBlue
再次,thx很多。由於你的回答,我決定在這裏修改json以獲得更多有用和簡單的編碼。 xD – BlitzkriegBlue
@ArthurAbreu沒問題!如果這個答案對你有幫助,你可以選擇這個作爲頁面上的正確答案。 –