50,527
社区成员
发帖
与我相关
我的任务
分享
package com.zzqq.simpleadapter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView) findViewById(com.zzqq.simpleadapter.R.id.lv);
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
int w;
for(w=0; w<20; w++)
{
Map<String,Object> map1 = new HashMap<String, Object>();
map1.put("name", "我的歌声里-曲婉婷");
map1.put("iconid",R.drawable.ic_menu_add);
data.add(map1);
}
Map<String,Object> map10 = new HashMap<String, Object>();
map10.put("name", "aaaaaaaaaaaaaa");
map10.put("iconid",R.drawable.ic_menu_add);
data.add(map10);
lv.setAdapter(new SimpleAdapter(this, data, R.layout.list_itme, new String[]{"name","iconid"}, new int[]{R.id.tv,R.id.iv}));
Toast.makeText(this, "列表加载完成!", Toast.LENGTH_LONG).show();
//Json解析
HttpClient client = new DefaultHttpClient();
StringBuilder builder = new StringBuilder();
HttpGet myget = new HttpGet("http://192.168.1.2/2/index.php");
try {
HttpResponse response = client.execute(myget);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
builder.append(s);
Log.v("测试", builder.toString());
}
JSONArray json = new JSONArray(builder.toString());
for(int i =0; i<json.length(); i++){
JSONObject object=json.getJSONObject(i);
String str = new String();
str=object.getString("username");
Toast.makeText(this, str, 1).show();
Log.v("我是来测试的", str);
}
/*
JSONObject jsonObject = new JSONObject(builder.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();
}
}
}
public static List<Affiliate> convert2AdConfig(String serverJsonStr)
throws JSONException {
AdConfig adconfig = new AdConfig();
JSONObject object = new JSONObject(serverJsonStr);
JSONArray listjson = object.getJSONArray("rations");
List<Affiliate> list = new ArrayList<Affiliate>();
listjson.length();
// 鐢熸垚Affiliate瀵硅薄
for (int i = 0; i < listjson.length(); i++) {
JSONObject object2 = listjson.getJSONObject(i);
Affiliate affiliate = new Affiliate();
// {"platformId":82,"key":"debug","key2":"debug","weight":50,"adapterName":""},
affiliate.setAdapterName(object2.getString("adapterName"));
affiliate.setId(object2.getString("platformId"));
affiliate.setKey1(object2.getString("key"));
affiliate.setKey2(object2.getString("key2"));
affiliate.setWeight(object2.getString("weight"));
System.out.println(affiliate.toString());
list.add(affiliate);
}
return list;
}
[/quote]好的 谢谢 我试试!
public static List<Affiliate> convert2AdConfig(String serverJsonStr)
throws JSONException {
AdConfig adconfig = new AdConfig();
JSONObject object = new JSONObject(serverJsonStr);
JSONArray listjson = object.getJSONArray("rations");
List<Affiliate> list = new ArrayList<Affiliate>();
listjson.length();
// 鐢熸垚Affiliate瀵硅薄
for (int i = 0; i < listjson.length(); i++) {
JSONObject object2 = listjson.getJSONObject(i);
Affiliate affiliate = new Affiliate();
// {"platformId":82,"key":"debug","key2":"debug","weight":50,"adapterName":""},
affiliate.setAdapterName(object2.getString("adapterName"));
affiliate.setId(object2.getString("platformId"));
affiliate.setKey1(object2.getString("key"));
affiliate.setKey2(object2.getString("key2"));
affiliate.setWeight(object2.getString("weight"));
System.out.println(affiliate.toString());
list.add(affiliate);
}
return list;
}
package com.zzqq.listmusic.json;
import org.json.JSONObject;
import com.zzqq.listmusic.domian.Person;
/**
* 完成对json数据的解析
* @author z
*
*/
public class JsonTool {
public JsonTool() {
}
public static Person getPerson(String key , String jsonString){
Person person = new Person();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject persJsonObject = jsonObject.getJSONObject("list");
person.setTitle(persJsonObject.getString("title"));
person.setSinger(persJsonObject.getString("singer"));
} catch (Exception e) {
}
return person;
}
}
你看result能否打印出来
已经成功了 谢谢 可视Json的时候取到的值都是空的,请教一下怎么解决 [quote=引用 4 楼 zzqqdwc 的回复:] [quote=引用 2 楼 huxiweng 的回复:] 192.168.1.2是你机器的IP的话就改成上面那个
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("xxxxxxxxxxxxxxx");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String strUrl = "http://146.11.24.100:9081/testcontroller/test1?arg=2222";
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
URL url = null;
try {
url = new URL(strUrl);
System.out.println(url.getPort());
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
BufferedReader br = new BufferedReader(in);
String result = "";
String readerLine = null;
while((readerLine=br.readLine())!=null){
result += readerLine;
}
in.close();
urlConn.disconnect();
System.out.println("r:"+result);
TextView textView = (TextView)this.findViewById(R.id.result);
textView.setText(result);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
已经成功了 谢谢 可视Json的时候取到的值都是空的,请教一下怎么解决 [quote=引用 2 楼 huxiweng 的回复:] 192.168.1.2是你机器的IP的话就改成上面那个