Android で非同期タスクを使用してオンライン データベースから json を取得し、ユーザーのパスワードとユーザー名が正しいかどうかを確認しています。
電話でテストするたびに、「ステータス」のjson文字列は常にいいえであり、正しい情報を入力しても決してはいではありません。
しかし、携帯電話に入力したのと同じ情報をブラウザでテストすると、正しい json の結果が得られます。常に「いいえ」を返しています
非同期タスク:
public class ReadLogInJSON extends AsyncTask
<String, Void, String> {
Context c;
public ReadLogInJSON(Context context)
{
c = context;
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}
protected void onPostExecute(String result){
//decode json here
try{
JSONObject json = new JSONObject(result);
String status = json.getString("status");
Log.d("BeforeIf", status);
if(status.equals("no")){
//toast logIN failed
Log.d("logIN-no", status);
String message = "Log In Failed";
Toast.makeText(c, message, Toast.LENGTH_SHORT).show();
}
else{
//get userName
Log.d("logIN-yes", "correct");
//get user ID
//set preferences
//launch normal activity
}
}
catch(Exception e){
}
}
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Failed to download file");
}
} catch (Exception e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();
}
}