[英]A very easy parse json PHP on Android
I am very new at this. 我对此很陌生。 I have already read and watch and try a lot tutorials, but I can't find something very easy in order to understand how it works.
我已经阅读,观看并尝试了很多教程,但是为了理解它的工作原理,我找不到非常简单的东西。
I have made a php
file where you can see the results of o form "result.php"
. 我已经制作了一个
php
文件,您可以在其中查看"result.php"
形式的结果。 In the form, you can insert the First-name
and Last-name
. 在表格中,您可以插入
First-name
和Last-name
。 Those are saved in the database. 那些被保存在数据库中。
In "result.php"
, you can see the results as: 在
"result.php"
,您可以看到以下结果:
{"nametable":[{"fname":"","lname":""},{"fname":"ni","lname":"gi"}]}
Now, I would like to find an easy way, in order to understand it, were on my Android code, there is going to be just a text view and show the first name.. I don't want to get confused with buttons, just a page where is going to show a variable from a database! 现在,我想在我的Android代码上找到一种简单的方法,以便理解它,它只是一个文本视图并显示名字。.我不想与按钮混淆,只是一个页面,它将显示数据库中的变量!
Is there anyone that can help me with code, in order to understand the way it works?? 有没有人可以帮助我编写代码,以了解其工作方式?
Thank you!! 谢谢!!
Here's a simple code sample I made, using org.json library & OkHttp client. 这是我使用org.json库和OkHttp客户端制作的一个简单代码示例。
It will retrieve the JSON from the server, and show every first name in TextViews. 它将从服务器检索JSON,并在TextViews中显示每个名字。
Note: I didn't test it, it may not compile, but you get the idea. 注意:我没有测试它,它可能无法编译,但是您明白了。
activity_main.xml activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textViewWrapper"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TextView's will be appended here -->
</LinearLayout>
MyActivity.java MyActivity.java
//...other imports
import org.json.*;
import okhttp3.*;
public class MyActivity extends AppCompatActivity {
private LinearLayout textViewWrapper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get TextView Wrapper
textViewWrapper = (LinearLayout) findViewById(R.id.textViewWrapper);
fillTextView();
}
/**
* Fill TextViews with API response.
*
*/
public void fillTextView() {
get("http://example.com/result.php", new Callback() {
@Override
public void onFailure(Call call, IOException e) {}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String body = response.body().string(); //Get API response
//OKHttp callback isn't in the main thread,
//UI operations need to be run in the main thread
//That's why you should use runOnUiThread
MyActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject JSON = null;
try {
//Parse JSON
JSON = new JSONObject(body);
//Get "nametable" array
JSONArray nameTable = JSON.getJSONArray("nametable");
//Loop through "nametable" array
for (int i = 0; i < nameTable.length(); i++) {
JSONObject item = nameTable.getJSONObject(i);
//Create TextView
TextView rowTextView = new TextView(MyActivity.this);
//Set fname
rowTextView.setText(item.getString("fname"));
// add the textview
textViewWrapper.addView(rowTextView);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
/**
* OKHttp GET helper function (You should probably put this in a Request Utils class)
*
*/
public Call get(String url, Callback callback) {
OkHttpClient client = new OkHttpClient();
okhttp3.Request request = new okhttp3.Request.Builder()
.url(url)
.get()
.build();
Call call = client.newCall(request);
call.enqueue(callback);
return call;
}
}
More info on parsing JSON in this question: How to parse JSON in Java 有关此问题中的JSON解析的更多信息: 如何在Java中解析JSON
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.