Java 无法在android TextView中打印JSON对象字符串
因此,我试图从一个网站获取JSON字符串,如下所示Java 无法在android TextView中打印JSON对象字符串,java,android,json,Java,Android,Json,因此,我试图从一个网站获取JSON字符串,如下所示 [{"name":"Painting"},{"name":"Painting or varnishing doors"},{"name":"Painting or varnishing frames"},{"name":"Varnishing floors"},{"name":"Picking old wallpaper"},{"name":"Painting the facade"},{"name":"professional athlete
[{"name":"Painting"},{"name":"Painting or varnishing doors"},{"name":"Painting or varnishing frames"},{"name":"Varnishing floors"},{"name":"Picking old wallpaper"},{"name":"Painting the facade"},{"name":"professional athlete"}]
我只想用字符串“Painting”获取第一个JSONObject
下面是我的MainActivity.java代码
package mobiletest.pixelapp.com.mobiletest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import model.Cup;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private String myString;
private String anotherString;
private String myVar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
Cup myCup = new Cup();
String newString = myCup.myMethod();
try {
JSONArray jsonArray = new JSONArray(newString);
JSONObject jsonObject = jsonArray.getJSONObject(0);
Log.v("Key",jsonObject.getString("name"));
textView.setText(jsonObject.getString("name"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
这是我的java类文件cup.java
package model;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
/**
* Created by pruthvi on 12/2/2015.
*/
public class Cup {
public String myMethod()
{
String output = getUrlContents("http://xyz.co/tests/android-query.php");
return output;
}
private static String getUrlContents(String theUrl)
{
StringBuilder content = new StringBuilder();
// many of these calls can throw exceptions, so i've just
// wrapped them all in one try/catch statement.
try
{
// create a url object
URL url = new URL(theUrl);
// create a urlconnection object
URLConnection urlConnection = url.openConnection();
// wrap the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
// read from the urlconnection via the bufferedreader
while ((line = bufferedReader.readLine()) != null)
{
content.append(line + "\n");
}
bufferedReader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return content.toString();
}
}
现在的问题是,当我以java的形式运行这段代码时,我可以很容易地从JSONObject打印绘画,但是当我试图通过设置TextView的文本以android视图的形式运行它时,我得到了一些奇怪的system.err
12-02 14:06:26.809 19250-19250/mobiletest.pixelapp.com.mobiletest D/libc: [NET] getaddrinfo hn 10, servname NULL, ai_family 0+
12-02 14:06:26.809 19250-19250/mobiletest.pixelapp.com.mobiletest W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:393)
12-02 14:06:26.809 19250-19250/mobiletest.pixelapp.com.mobiletest W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:244)
12-02 14:06:26.809 19250-19250/mobiletest.pixelapp.com.mobiletest W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:219)
我不熟悉java和android,现在我只想从远程服务器文件和数据库中获取数据
提前谢谢你你喜欢那种一次性的方法吗
try {
JSONArray jsonArray = new JSONArray(newString);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name")
textView.setText(name));}
} catch (JSONException e) {
e.printStackTrace();
}
试试看{
JSONArray JSONArray=新JSONArray(新字符串);
for(int i=0;i
它将把name设置为textView。
乐于帮助,乐于编码在onCrate方法中这样做
try {
JSONArray jsonArray = new JSONArray(newString);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name")
textView.setText(name));}
} catch (JSONException e) {
e.printStackTrace();
}
试试看{
JSONArray JSONArray=新JSONArray(新字符串);
for(int i=0;i
它将把name设置为textView。
乐于帮助和乐于编码看看这个例子,它会给你一个想法
AsyncTask<Void, Void, Void> asyncLoad = new AsyncTask<Void, Void, Void>()
{
@Override
protected Void doInBackground(Void... params)
{
URL url = new URL("http://www.omdbapi.com/?i=&t="
+ TITLE);
String URL2="http://www.omdbapi.com/?i=&t=saw";
Log.d("URL content", url.toString());
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
Log.d("URL content", "register URL");
urlConnection.connect();
Log.d("URL connection", "establish connection");
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
}
};
asyncLoad.execute();
AsyncTask asyncLoad=new AsyncTask()
{
@凌驾
受保护的Void doInBackground(Void…参数)
{
URL=新URL(“http://www.omdbapi.com/?i=&t="
+头衔);
字符串URL2=”http://www.omdbapi.com/?i=&t=saw";
Log.d(“URL内容”,URL.toString());
HttpURLConnection urlConnection=(HttpURLConnection)url
.openConnection();
Log.d(“URL内容”、“注册URL”);
urlConnection.connect();
Log.d(“URL连接”、“建立连接”);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果)
{
super.onPostExecute(结果);
}
};
asyncLoad.execute();
看看这个例子,它会给你一个想法
AsyncTask<Void, Void, Void> asyncLoad = new AsyncTask<Void, Void, Void>()
{
@Override
protected Void doInBackground(Void... params)
{
URL url = new URL("http://www.omdbapi.com/?i=&t="
+ TITLE);
String URL2="http://www.omdbapi.com/?i=&t=saw";
Log.d("URL content", url.toString());
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
Log.d("URL content", "register URL");
urlConnection.connect();
Log.d("URL connection", "establish connection");
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
}
};
asyncLoad.execute();
AsyncTask asyncLoad=new AsyncTask()
{
@凌驾
受保护的Void doInBackground(Void…参数)
{
URL=新URL(“http://www.omdbapi.com/?i=&t="
+头衔);
字符串URL2=”http://www.omdbapi.com/?i=&t=saw";
Log.d(“URL内容”,URL.toString());
HttpURLConnection urlConnection=(HttpURLConnection)url
.openConnection();
Log.d(“URL内容”、“注册URL”);
urlConnection.connect();
Log.d(“URL连接”、“建立连接”);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果)
{
super.onPostExecute(结果);
}
};
asyncLoad.execute();
您不能在UI线程中执行网络任务。
所以
工作不正常
这些错误的主要原因与线程上下文有关
如果您想使用android执行网络任务,请使用async task或其他网络库(我个人建议
改型
)。您不能在UI线程中执行网络任务。
所以
工作不正常
这些错误的主要原因与线程上下文有关
如果您想使用android执行网络任务,请使用async task或其他网络库(我个人建议改型
)
在onCreate()外部定义方法displayName(String)
在onCreate()外部定义方法displayName(String)
您的服务器名称值为空。。检查值同时给定的URL给出了404错误。试着在子线程中运行服务器端代码,而不是在主线程上。正如我所说,这个程序与Java程序运行得相当好,但与android不一样。我可以从服务器获取字符串并打印它。为了更好地使用
asynctask
。您的服务器名称值为空。。检查值同时给定的URL给出了404错误。试着在子线程中运行服务器端代码,而不是在主线程上。正如我所说,这个程序与Java程序运行得相当好,但与android不一样。我可以从我的服务器获取字符串并打印它。为了更好地使用asynctask
。如果您看到JSON字符串,您将找到具有相同关键字“name”的所有对象。然后如何在for循环中运行它并设置文本?当我在主xml文件中创建一个TextView时,如果必须显示json数组的所有项,则必须使用listView显示所有名称。您实际想要做什么?如果您看到JSON字符串,您将找到所有具有相同关键字“name”的对象。然后如何在for循环中运行它并设置文本?当我在主xml文件中创建一个TextView时,如果必须显示json数组的所有项,则必须使用listView显示所有名称。你到底想做什么?
public void displayName(final String name){
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(jsonObject.getString("name"));
}
});
}