Android訪問(wèn)php取回json數(shù)據(jù)實(shí)例
$array = array(
'username'=>'楊鑄',
'password'=>'123456',
'user_id'=>1
);
echo json_encode($array);
java代碼
private void startUrlCheck(String username,String password)
{
HttpClient client = new DefaultHttpClient();
StringBuilder builder = new StringBuilder();
HttpGet myget = new HttpGet("http://10.0.2.2/Android/index.php");
try {
HttpResponse response = client.execute(myget);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
builder.append(s);
}
JSONObject jsonObject = new JSONObject(builder.toString());
String re_username = jsonObject.getString("username");
String re_password = jsonObject.getString("password");
int re_user_id = jsonObject.getInt("user_id");
setTitle("用戶id_"+re_user_id);
Log.v("url response", "true="+re_username);
Log.v("url response", "true="+re_password);
} catch (Exception e) {
Log.v("url response", "false");
e.printStackTrace();
}
}
運(yùn)行說(shuō)明
其中http://10.0.2.2為Android訪問(wèn)本機(jī)url的ip地址。對(duì)應(yīng)電腦上測(cè)試的http://127.0.0.1
另外執(zhí)行代碼時(shí)會(huì)拋出異常
java.net.SocketException: Permission denied
此為應(yīng)用訪問(wèn)網(wǎng)絡(luò)的權(quán)限不足 在AndroidManifest.xml中,需要進(jìn)行如下配置:
<uses-permission Android:name="android.permission.INTERNET" />
就加在
</manifest>
之前就好了
然后測(cè)試通過(guò)。
- Android App端與PHP Web端的簡(jiǎn)單數(shù)據(jù)交互實(shí)現(xiàn)示例
- Android md5加密與php md5加密一致詳解
- Android上傳文件到Web服務(wù)器 PHP接收文件
- Android通過(guò)PHP服務(wù)器實(shí)現(xiàn)登錄功能
- PHP簡(jiǎn)單判斷iPhone、iPad、Android及PC設(shè)備的方法
- php、java、android、ios通用的3des方法(推薦)
- 基于PHP后臺(tái)的Android新聞瀏覽客戶端
- php生成Android客戶端掃描可登錄的二維碼
- Android異步上傳圖片到PHP服務(wù)器
- 使用PHP開(kāi)發(fā)Android應(yīng)用程序技術(shù)介紹
- android+json+php+mysql實(shí)現(xiàn)用戶反饋功能方法解析
- Android和PHP MYSQL交互開(kāi)發(fā)實(shí)例
相關(guān)文章
AndroidStudio升級(jí)4.1后啟動(dòng)失敗Plugin問(wèn)題解決
這篇文章主要介紹了AndroidStudio升級(jí)4.1后啟動(dòng)失敗Plugin問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Android 動(dòng)態(tài)注冊(cè)監(jiān)聽(tīng)網(wǎng)絡(luò)變化實(shí)例詳解
這篇文章主要介紹了Android 動(dòng)態(tài)注冊(cè)監(jiān)聽(tīng)網(wǎng)絡(luò)變化實(shí)例詳解的相關(guān)資料,這里提供簡(jiǎn)單實(shí)例及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2017-07-07懸浮對(duì)話框Android代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了懸浮對(duì)話框Android代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Android Studio出現(xiàn)Failed to pull selection: open failed: Permi
本篇文章給大家分享了Android Studio中導(dǎo)出數(shù)據(jù)庫(kù)文件的方法以及出現(xiàn)Failed to pull selection: open failed: Permission denied的解決思路,有興趣的學(xué)習(xí)下。2018-05-05詳解Android6.0運(yùn)行時(shí)權(quán)限管理
自從Android6.0發(fā)布以來(lái),在權(quán)限上做出了很大的變動(dòng),不再是之前的只要在manifest設(shè)置就可以任意獲取權(quán)限,而是更加的注重用戶的隱私和體驗(yàn)。本文詳細(xì)介紹了Android6.0運(yùn)行時(shí)權(quán)限管理。需要的朋友一起來(lái)看下吧2016-12-12Android adb logcat 命令查看日志詳細(xì)介紹
這篇文章主要介紹了Android adb logcat 命令詳細(xì)介紹的相關(guān)資料,這里對(duì)logcat 命令進(jìn)行了詳細(xì)介紹,并介紹了過(guò)濾日志輸出的知識(shí),需要的朋友可以參考下2016-12-12kotlin 官方學(xué)習(xí)教程之基礎(chǔ)語(yǔ)法詳解
這篇文章主要介紹了kotlin 官方學(xué)習(xí)教程之基礎(chǔ)語(yǔ)法詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05android studio 新手入門(mén)教程(二)項(xiàng)目的導(dǎo)入教程圖解
這篇文章主要介紹了android studio 新手入門(mén)教程(二)項(xiàng)目的導(dǎo)入教程圖解,需要的朋友可以參考下2017-12-12