Working on Android projects is inseparable from going to the server to retrieve data. A typical example is Android accessing php to retrieve json data. There are a lot of similar examples on the Internet, and the codes are all the same. I want to complain. The codes you posted are incomplete. Isn't this a scam?
To do this project, we need to use the dependency packages (jar packages) provided by Apache: ①httpclient ②httpcore ③http-mimi ④apache-mime4j
International practice: go to DEMO first, download address: Android access php to call json Data
Let’s first familiarize ourselves with the json data format under PHP
e.g.
$tnnowu = array( 'username' => '灬抹茶灬', 'password' => '666', 'user_id' => 1 ); echo json_encode($tnnowu);
MainActivity.java
package com.cnwuth.getjson; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONObject; import java.io.BufferedReader; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } private void startURLCheck(String username,String password) { HttpClient httpClient = new DefaultHttpClient(); StringBuilder stringBuilder = new StringBuilder(); HttpGet httpGet = new HttpGet("xxx.xxx.php"); try { HttpResponse httpResponse = httpClient.execute(httpGet); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( httpResponse.getEntity().getContent() )); for (String s = bufferedReader.readLine();s!=null;s=bufferedReader.readLine()) { stringBuilder.append(s); } JSONObject jsonObject = new JSONObject(stringBuilder.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(); } } }
AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cnwuth.getjson"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
The above introduces how Android accesses PHP to retrieve json data, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.