android從網路獲取數據
A. android如何從一個網路介面中獲得json數據,並且進行解析呢
你可以用jsonsmart來解析: JSONValue.parseStrict(返回的數據) 進行解析,之後再根據json欄位拿數據
B. android怎麼從網路獲取數據
通過線程 和HTTPclient 來獲取網路數據的
建議看看視頻教程 luo.apkbus.com/
C. android怎麼獲取網頁數據
下面介紹三種獲取網頁數據的代碼
例子來自於android學習手冊,android學習手冊包含9個章節,108個例子,源碼文檔隨便看,例子都是可交互,可運行,源碼採用android studio目錄結構,高亮顯示代碼,文檔都採用文檔結構圖顯示,可以快速定位。360手機助手中下載,圖標上有貝殼
//第一種
/**獲取參數(ArrayList<NameValuePair> nameValuePairs,String url)後post給遠程伺服器
* 將獲得的返回結果(String)返回給調用者
* 本函數適用於查詢數量較少的時候
*/
public String posturl(ArrayList<NameValuePair> nameValuePairs,String url){
String result = "";
String tmp= "";
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
return "Fail to establish http connection!";
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
tmp=sb.toString();
}catch(Exception e){
return "Fail to convert net stream!";
}
try{
JSONArray jArray = new JSONArray(tmp);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Iterator<?> keys=json_data.keys();
while(keys.hasNext()){
result += json_data.getString(keys.next().toString());
}
}
}catch(JSONException e){
return "The URL you post is wrong!";
}
return result;
}
//第二種
/**獲取參數指定的網頁代碼,將其返回給調用者,由調用者對其解析
* 返回String
*/
public String posturl(String url){
InputStream is = null;
String result = "";
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
return "Fail to establish http connection!"+e.toString();
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-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){
return "Fail to convert net stream!";
}
return result;
}
//第三種
/**獲取指定地址的網頁數據
* 返回數據流
*/
public InputStream streampost(String remote_addr){
URL infoUrl = null;
InputStream inStream = null;
try {
infoUrl = new URL(remote_addr);
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
inStream = httpConnection.getInputStream();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inStream;
D. android 手機怎麼連接電腦上的資料庫,直接獲取資料庫數據
資料庫很多,但是安卓直接支持的資料庫只有sqlite一種。如果要使用其它的非本地資料庫,你得建立連接,採用webservice或http協議中轉數據。
安卓訪問mysql里的數據得:
1)先建立網路,使用WIFI區域網
2)在PC建立數據訪問介面,編寫http協議應用(方法非常多。
asp、php、jsp都有訪問mysql的方法),還得關掉防火牆
3)編寫安卓手機客戶端連接設備的http
service