【问题标题】:How to Android authentication and take data with Json?如何使用 Json 进行 Android 身份验证并获取数据?
【发布时间】:2015-03-13 22:45:45
【问题描述】:

我使用 web 服务创建到 android 身份验证的项目。我使用 post 和 json 解析来获取数据,我发送到 web 服务用户名和密码进行身份验证,这些用户名和密码获取我想要在 pesponse 数据中选择的任何信息,但无论我没有成功获取我想要的我想我犯了一些错误或忘记了一些东西。我想登录到网络身份验证和身份验证后,如何获取我想要的特定信息。

class Post extends AsyncTask<Void, Void, Void>{
    // Post tan önce yapılacak işlemler. Yükleniyor yazısını(ProgressDialog) gösterdik.
    JSONObject veri_json;
    JSONObject jsonobj;
    protected void  onPreExecute() {

        pDialog=new ProgressDialog(OgrenciBilgiSistemi.this);
        pDialog.setMessage("Yükleniyor");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(true);// ProgressDialog u iptal edilemez hale getirdik.
        pDialog.show();
    };
    @Override
    protected Void doInBackground(Void... unused) {// Arka Planda yapılacaklar. Yani Post işlemi 

        //Post edilecek değişkenleri ayarliyoruz.
        List<NameValuePair> params=new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("login", og_no.getText().toString()));
        params.add(new BasicNameValuePair("password", og_sifre.getText().toString()));
        params.add(new BasicNameValuePair("Accept", "application/json"));
        params.add(new BasicNameValuePair("Content-type", "application/json"));


        //PostClass daki httpPost metodunu çağırdık.Gelen string değerini aldık
        veri_string=post.httpPost(url, "POST", params, 2000);

        try {
            ////gelen veri_string değerini json arraye çeviriyoruz.
             //try içinde yapmak zorunlu çıkabilecek bir sorunda uygulamanın patlamaması için
            veri_json=new JSONObject(veri_string);




        } catch (JSONException e) {
            e.printStackTrace();
        }
        // gelen veriyi log tuttuk
        Log.d("HTTP POST CEVAP:",""+ veri_string);
        return null;
    }

我有邮政类

try {

        if (method == "POST") {

            HttpParams httpParameters = new BasicHttpParams();
            int timeout1 = time;
            int timeout2 = time;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeout1);
            HttpConnectionParams.setSoTimeout(httpParameters, timeout2);
            DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
            HttpPost httpPost = new HttpPost(url);  
            httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            veri =  httpEntity.getContent();

        } else if (method == "GET") {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            veri =  httpEntity.getContent();            
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                veri, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        veri.close();
        veri_string = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Hata " + e.toString());
             }

    return veri_string; // Aldığımız cevabın string halini geri dönüyoruz

}

我接受这个 pesponse

【问题讨论】:

    标签: android json post


    【解决方案1】:

    您可以在 onPostExecute 中解析日期 首先创建 JSONobject myObject=new JSONobject(来自服务器的 json 字符串) 然后你可以通过调用 myObject.getJSONArray("name of the array"), myObject.getJSONObject("object name") 来提取你的数组或内部 json 对象 然后从这些对象和数组中,您可以通过调用适当的方法(如 getString getInt 等)来提取值。 提取所有这些信息后,将这些值存储在 SharedPreferences 中,以便在应用程序的任何位置使用。 JSON 解析和 SharedPreferences 的链接 JSON parsing

    SharedPreferences

    【讨论】:

    • 你能给我具体的例子吗@Krishna Teja Velagapudi
    • 从上面的 json 文件中你发布了你想要提取的内容。我可以通过提取该文本来举个例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    相关资源
    最近更新 更多