android打印json对象类型,无法打印JSON对象的字符串在android系统的TextView

[{"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"}]

我只是想获取字符串“绘画”第一的JSONObject获取JSON字符串。

这里是我的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的新手,截至目前我只想从远程服务器文件和数据库获取数据。

在此先感谢

+0

您的服务器名称值为null ..检查值 –

+0

同样给定的URL是给404错误.. –

+0

尝试运行你的服务器端代码里面的子线程不在主线程 –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值