私の Android アプリでは、API への GET リクエストを使用して Web サイトにアクセスしています。リクエストは、さまざまなユーザー情報を含む HTML ファイルを返します。これから、プロフィール写真やデータなどのユーザーデータを抽出する方法を知りたいです。このデータにアクセスして取得し、アプリに表示する方法を知りたいです。
どんな助けでも大歓迎です。
前もって感謝します。
データにアクセスできるようにしたいだけの場合は、APIをJSON形式で出力し、アプリからJSONを読み取ることができます。
public void getData(){
String result = "";
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://website.com/download.html");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
names.add(" " + json_data.getString("name"));
age.add(" " + Integer.toString(json_data.getInt("age")));
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
最も簡単な方法は、HTML データを WebView に表示することです。