【问题标题】:Making Attendance App in Android在Android中制作考勤应用程序
【发布时间】:2017-08-10 14:17:54
【问题描述】:

我必须为大学制作考勤应用程序。该应用程序将从大学网站获取数据并根据用户登录名和密码将其显示在应用程序上。

当我们登录大学的网站时,我们必须在我的应用上输入 idpassword,以便用户可以在应用本身上看到它。

我搜索过httpurlconnectionhttpgethttppostjsoup

到现在为止,我已经明白我必须让httprequest加载学院的考勤网站,然后httppost发布用户名和密码,然后jsoup从HTML页面获取数据。

但是我看到的教程只是请求 JSON 页面,但是如何请求 HTML 页面?并发布登录到它?

这是我尝试并从 JSON 收集数据的方法

private  TextView textresponse1;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button Get= (Button) findViewById(R.id.httprequest);
      textresponse1= (TextView)findViewById(R.id.textresponse);
      progressDialog=new ProgressDialog(this);
      Get.setOnClickListener(this);
}

@Override
public void onClick(View v) {    

      new JSONTask().execute("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoList.txt");
      progressDialog.setMessage("Collecting Data");
      progressDialog.show();
 }

public class JSONTask extends AsyncTask<String,String,String >{

    @Override
    protected String doInBackground(String... params) {
        BufferedReader reader = null;
        HttpURLConnection connection = null;
        try {
              URL url=new URL(params[0]);
              connection=(HttpURLConnection) url.openConnection();
              connection.connect();

              InputStream stream=connection.getInputStream();

              reader=new BufferedReader(new InputStreamReader(stream));

              String line="";
              StringBuffer buffer=new StringBuffer();
              while ((line = reader.readLine())!=null) {
                    buffer.append(line);
                }

              String finaljosn=buffer.toString();
              StringBuffer add =new StringBuffer();
              JSONObject parentobject=new JSONObject(finaljosn);
              JSONArray parentarray=parentobject.getJSONArray("movies");
              for(int i=0;i<parentarray.length();i++) {
                   JSONObject moviename = parentarray.getJSONObject(i);

                   String finalmovie = moviename.getString("movie");
                   int finalyear = moviename.getInt("year");
                   add.append(finalmovie +"- "+finalyear + "\n");
               }
                 return add.toString();
              // return finalmovie +" -Rushabh- " +finalyear;

         } catch (MalformedURLException e) {
              e.printStackTrace();
         } catch (IOException e) {
              e.printStackTrace();
         } catch (JSONException e) {
              e.printStackTrace();
         } finally {
              if (connection!=null) {
                  connection.disconnect();
              }
              try {
                    if (reader!=null) {
                        reader.close();
                   }
              } catch (IOException e) {
                    e.printStackTrace();
              }
           }
          return null;
       }

    @Override
    protected void onPostExecute(String result) {
          super.onPostExecute(result);
          progressDialog.dismiss();
          textresponse1.setText(result);
    }
}

【问题讨论】:

    标签: java android json http


    【解决方案1】:

     String mLoadURL="http://www.google.com";
     
     public class LoadHtml extends AsyncTask<String, String, String> {
    
            @Override
            protected String doInBackground(String... params) {
                URL url = null;
                try {
                    url = new URL(mLoadURL);
                    StringBuilder stringBuilder = new StringBuilder();
                    URLConnection conn = url.openConnection();
                    // Get the response
                    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String line = "";
                    while ((line = rd.readLine()) != null) {
                        stringBuilder.append(line);
                    }
                    return stringBuilder.toString();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return "";
            }
    
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                 if (!s.trim().isEmpty()) {
                   //load html to webview
                 }
            }
        }

    使用以下方法调用 AsyncTask 之上:

    LoadHtml loadHtml= new LoadHtml();
    load.execute();
    

    【讨论】:

    • load.execute 还是 loadHtml.execute?
    • 我还想把用户名和密码发给它
    • 对不起它的 loadHtml.execute();当网站加载时,表单也将被加载,用户将自行输入他们的用户名和密码。
    • 如何在我想知道的应用程序上加载 html 页面,然后将用户名和 id 发布到它
    • 您想自动发布用户名和密码吗?在 webview 中加载上述响应。
    猜你喜欢
    • 2015-12-28
    • 2012-04-01
    • 2016-05-17
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    相关资源
    最近更新 更多