如何在Buffer Reader Android中访问特定的字符串值

问题描述 投票:-1回答:1

我在Android设备中有下面的代码,该代码根据user_namepassword的发布值从我的Php页面返回一些结果

try {
            String user_name = params[1];
            String password = params[2];
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
                    +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String result="";
            String line="";
            while((line = bufferedReader.readLine())!= null) {
                result += line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Login Status");
}

@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();

}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

在postExecute上它显示如下结果

enter image description here

我需要访问仅user_iduser_mobile之类的特定值我该如何访问呢?不知道

android bufferedreader
1个回答
0
投票
try { JSONArray arr = new JSONArray(result); JSONObject jObj = arr.getJSONObject(0); String userId = jObj.getString("user_id"); } catch (JSONException e) { Log.e(TAG, "onPostExecute: " + e); }
© www.soinside.com 2019 - 2024. All rights reserved.