如何在Android中存储HTTP URL的响应 [英] How to store the response of an HTTP url in android

查看:259
本文介绍了如何在Android中存储HTTP URL的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Android应用程序,用户在其中输入名称,电子邮件ID,这些文件将解析为URL,并且所有用户数据都存储在其中.

I am building an android application where user enter the name, email ID This are parse to URL and all the user data are store there.

现在,当用户发送此信息时,服务器上此时会生成一个ID,它会向我发送该唯一ID作为URL响应.

Now, When an user send this info an ID is generated at that Time on server and it resend to me that unique ID as the URL response.

我如何获得此响应并将其存储.因此,我可以将其用于将来使用.

How can I get this response and store it. So I can use it For my future use.

这是我的代码,用于将该值解析为该URL-

Here is my code for parsing this value to that URL -

private class DownloadJSON extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog

    }

    @Override
    protected String doInBackground(String... params) {

        // Create an array
        //public String postData() {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://URL/api/user");

          try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>2);
            nameValuePairs.add(new BasicNameValuePair("name", name));
            nameValuePairs.add(new BasicNameValuePair("email", email));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            InputStream parse = entity.getContent();

            return getStringFromInputStream(parse);

          } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
           } catch (IOException e) {
            // TODO Auto-generated catch block
          }

            return "No Result !";
        } 

         //convert InputStream to String
        private  String getStringFromInputStream(InputStream is) {

            BufferedReader br = null;
            StringBuilder sb = new StringBuilder();

            String line;
            try {

                br = new BufferedReader(new InputStreamReader(is));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            return sb.toString();
          }


    @Override
    protected void onPostExecute(String args) {

    }
}

我是Android新手,请帮助我!

I am new to android please help me!!

推荐答案

更好地使用 SharedPreferences 即可使用众所周知的密钥保存所有内容.

Better to use SharedPreferences to save all with a well known key.

首先将您的userName,EmailId保存在sharedpreferences中,类似

First save your userName, EmailId in the sharedpreferences , something like

//Intialize it first
SharedPreferences sharedpreferences = getSharedPreferences("SOME_KEY", Context.MODE_PRIVATE);

//Use the Editor to put value inside this
Editor editor = sharedpreferences.edit();
//save both the name and email first 
editor.putString("NAME", name);
editor.putString("EMAILID", email);
//finally commit it
editor.commit();

现在,您的服务器了吗?

Now do your server part...

@Override 
protected String doInBackground(String... params) {

    // Create an array 
    //public String postData() { 
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://URL/api/user");

      try { 
        // Add your data 
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>2);
        nameValuePairs.add(new BasicNameValuePair("name", name));
        nameValuePairs.add(new BasicNameValuePair("email", email));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request 
        HttpResponse response = httpclient.execute(httppost);
        String responseId = response.getEntity().getContent();

        //Just save your ID in the same sharedPreferences in UI Thread
        runOnUiThread(new Runnable(){
          public void run() { 
            Editor editor = sharedpreferences.edit();
            //save your ID now
           editor.putString("ID", responseId );
           editor.commit();
          } 
       }); 
       ..............................
       ..............................

只要需要,只需使用相关密钥从SharedPreference中获取即可.

Whenever you need it, just get it from your SharedPreference with the relevant key.like

      String id = sharedpreferences.getStringExtra("ID", "Some default value");

这篇关于如何在Android中存储HTTP URL的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆