Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中使用Gson进行Json Post_Java_Android_Json_Gson - Fatal编程技术网

Java 在Android中使用Gson进行Json Post

Java 在Android中使用Gson进行Json Post,java,android,json,gson,Java,Android,Json,Gson,根据我不完全理解的教程,我做了以下工作: 我创建了四个类 package com.example.android.wearable.watchface; import com.google.gson.annotations.SerializedName; import java.util.ArrayList; public class Beansubject { @SerializedName("subj

根据我不完全理解的教程,我做了以下工作:

我创建了四个类

        package com.example.android.wearable.watchface;
        import com.google.gson.annotations.SerializedName;
        import java.util.ArrayList;

        public class Beansubject {

            @SerializedName("subject")
            private static String subject;
            @SerializedName("price")
            private static String price;
            @SerializedName("author")
            private static String author;
            @SerializedName("topics")
            private static ArrayList<BeanTopic> beanTopics;

            public Beansubject(ArrayList<BeanTopic> beanTopics, String subject, String price, String author) {
                this.beanTopics = beanTopics;
                this.subject = subject;
                this.price = price;
                this.author = author;
            }

            public static String getSubject() {
                return subject;
            }
            public static void setSubject(String subj) {
               subject = subj;
            }
            public static String getPrice() {
                return price;
            }
            public static void setPrice(String pric) {
                price = pric;
            }
            public String getAuthor() {
                return author;
            }
            public static void setAuthor(String aue) {
            author = aue;
            }
            public static ArrayList<BeanTopic> getBeanTopics() {
                return beanTopics;
            }
            public static void setBeanTopics(ArrayList<BeanTopic> beantcs) {
                beanTopics = beantcs;
            }
        }
下一个

最后

    package com.example.android.wearable.watchface;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.AsyncTask;
    import android.util.Log;
    import android.widget.TextView;

    import com.google.gson.GsonBuilder;

    import java.io.Reader;
    import java.util.ArrayList;


    public class MainActivity extends Activity {


        ProgressDialog progressDialog;
        TextView txtSubject;
        TextView txtAuthor;
        TextView txtPrice;
        TextView txtTopicsList;
        Beansubject beanSubject;
        StringBuffer topicList;

        public void Json() {

            new AsyncTask<Void,Void,Void>(){
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
                    progressDialog.setCancelable(false);
                    progressDialog.setMessage("Loading...");
                    progressDialog.show();
                }
                @Override
                protected Void doInBackground(Void... voids) {
                    Reader reader=API.getData("http://beta.json-generator.com/api/json/get/OkS85Le");
                    beanSubject = new GsonBuilder().create().fromJson(reader, Beansubject.class);
                    Log.e("Subject: ", beanSubject.getSubject() + "");
                    Log.e("Author: ", beanSubject.getAuthor() + "");
                    Log.e("Price: ", beanSubject.getPrice() + "");
                    ArrayList<BeanTopic> topicArrayList = beanSubject.getBeanTopics();
                    topicList = new StringBuffer();
                    for(BeanTopic topic: topicArrayList){
                        Log.e("topic title: ",topic.getTitle()+"");
                        topicList.append("* " + topic.getTitle()+"\n");
                    }
                    return null;
                }
                @Override
                protected void onPostExecute(Void aVoid) {
                    super.onPostExecute(aVoid);
                    progressDialog.dismiss();
                    txtSubject.setText("Subject: " + beanSubject.getSubject());
                    txtPrice.setText("price: "+beanSubject.getPrice());
                    txtAuthor.setText("Author: "+beanSubject.getAuthor());
                    txtTopicsList.setText("Topics: "+"\n" + topicList);
                }
            }.execute();

        }

    }
package com.example.android.wearable.watchface;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.util.Log;
导入android.widget.TextView;
导入com.google.gson.GsonBuilder;
导入java.io.Reader;
导入java.util.ArrayList;
公共类MainActivity扩展了活动{
进行对话进行对话;
TextView txtSubject;
TextView-txtAuthor;
TextView txtPrice;
TextView txtTopicsList;
beansobject beansobject;
主题列表;
public-void-Json(){
新建异步任务(){
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
ProgressDialog=新建ProgressDialog(MainActivity.this);
progressDialog.setCancelable(假);
progressDialog.setMessage(“加载…”);
progressDialog.show();
}
@凌驾
受保护的空位背景(空位…空位){
Reader=API.getData(“http://beta.json-generator.com/api/json/get/OkS85Le");
beansobject=new GsonBuilder().create().fromJson(reader,beansobject.class);
Log.e(“Subject:,beansobject.getSubject()+”);
Log.e(“Author:,beansobject.getAuthor()+”);
Log.e(“Price:,beansobject.getPrice()+”);
ArrayList topicArrayList=beansobject.getBeanTopics();
topicList=新的StringBuffer();
for(主题:主题列表){
Log.e(“主题标题:”,topic.getTitle()+”);
topicList.append(“*”+topic.getTitle()+“\n”);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
progressDialog.disclose();
setText(“主题:+beansObject.getSubject());
setText(“价格:+beansobject.getPrice());
setText(“作者:+beansobject.getAuthor());
setText(“主题:“+”\n“+topicList”);
}
}.execute();
}
}
错误在于我不确定如何创建活动类最后一部分教程中的对象。 对于在本教程的最后一部分中我不理解的部分,我随机猜测了对象是什么,这些对象可能链接到其他类,也可能不链接到其他类。 我甚至在其他类中使一些内容保持静态,并删除“this.”以使某些内容正常工作。请帮助我理解教程的最后一部分,以便我能够完全理解如何完成这个简单的任务。谢谢

以下是指向此简单教程的链接:


好的,这是我的未知变量:MyActivity.this,progressdialog,txtpostlist,postlist,BeanPostarayList。我现在使用的是不同的教程->更基本。因此,我不确定为什么教程在创建MainActivity类时如此含糊。少了什么

您需要创建一个布局,其中包含一些具有相应ID的TextView组件。因此,创建一个xml文件,其中包含一些具有适当ID的文本视图。然后重写onCreate方法,并调用JSON方法:

@Override
public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.id.my_layout);
    txtSubject = (TextView) findViewById(R.id.txtSubject);
    ...
    Json();
}

如果有什么不清楚的地方,我建议您阅读一本基本的android教程(来自官方网站的教程很棒)。

可能有助于查看API调用提供的数据

将结果粘贴到下面

旋度-v

因此,JSON中的3个“title”元素在UI中结束,原因如下:

beanSubject = new GsonBuilder().create().fromJson(reader, Beansubject.class);

ArrayList<BeanTopic> topicArrayList = beanSubject.getBeanTopics();

for(BeanTopic topic: topicArrayList){
                    topicList.append("* " + topic.getTitle()+"\n");
                }

txtTopicsList.setText("Topics: "+"\n" + topicList);
beansobject=new GsonBuilder().create().fromJson(reader,beansobject.class);
ArrayList topicArrayList=beansobject.getBeanTopics();
for(主题:主题列表){
topicList.append(“*”+topic.getTitle()+“\n”);
}
setText(“主题:“+”\n“+topicList”);
代码获取数据

从JSON数组创建的MVC模型/集合OBJ

集合obj中的UI列表集


有关从收集对象持有者获取数据的适配器的详细信息。

请帮助我修复此代码!你还没有告诉我们它是如何被破坏的。错误很简单,就是那些(活动类的)对象,我不知道如何创建它们,因为教程对活动类的最后一部分含糊不清。我不太擅长这一点。要创建哪种类型的XML文件(有几种)?最后,我该如何创建文本视图?@Newton Check这一点解释得很好,对我来说非常有意义。然而,当我在编辑手表面板时,布局下的XML不是文本可编辑的,我看到了一张手机图片,上面有其他东西。我不明白你的意思。更不用说我在那里找到了一个编辑文本的地方,我只希望它能像任何地方一样工作。
@Override
public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.id.my_layout);
    txtSubject = (TextView) findViewById(R.id.txtSubject);
    ...
    Json();
}
{"topics": [{"title": "how to add fontawesome icons in android example"},
            {"title": "how to parse json parsing using gson library"},
            {"title": "how to store arraylist in sharedpreferences"}],
 "auther": "nirav kalola",
 "price": "free",
 "subject": "Android Application Development Tutorial"}
beanSubject = new GsonBuilder().create().fromJson(reader, Beansubject.class);

ArrayList<BeanTopic> topicArrayList = beanSubject.getBeanTopics();

for(BeanTopic topic: topicArrayList){
                    topicList.append("* " + topic.getTitle()+"\n");
                }

txtTopicsList.setText("Topics: "+"\n" + topicList);