Android已经添加了网络权限为什么不能http访问网络

饭在桌上 2013-10-21 09:37:03
请教大神!!!!!!!!!!


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();


}


}
}





...全文
1759 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
饭在桌上 2013-10-23
  • 打赏
  • 举报
回复
引用 20 楼 huxiweng 的回复:
[quote=引用 19 楼 zzqqdwc 的回复:] [quote=引用 17 楼 AA5279AA 的回复:] [quote=引用 5 楼 zzqqdwc 的回复:] [quote=引用 3 楼 AA5279AA 的回复:] 加了网络权限了,那让我看看你的manifest.xml文件。。 另外,你连接的是局域网还是互联网。 用的是模拟器还是真机? 这效果都不一样的。。
已经添加了 现在已经能访问网络了,谢谢。 可是Json的时候取到的值都是空的,请教一下怎么解决[/quote] 取到的json首先看看字符串是不是空的。 我看你访问的是php,我不知道你的那个php会不会响应请求返回json传。[/quote]我那个php返回是有json数据的 可是取回来的是空的 搜狗的这个地址也是 http://music.sogou.com/sogou_phb/coo/music/getCampMeta2.jsp?cate=randomsong&count=100&jsoncallback=xiexiebangmang[/quote] 你怎么取的?这个肯定可以获取到[/quote] http://pan.baidu.com/s/1vcEQr 这是我的源码 请帮我修正一下 谢谢!
失落夏天 2013-10-23
  • 打赏
  • 举报
回复
引用 19 楼 zzqqdwc 的回复:
[quote=引用 17 楼 AA5279AA 的回复:] [quote=引用 5 楼 zzqqdwc 的回复:] [quote=引用 3 楼 AA5279AA 的回复:] 加了网络权限了,那让我看看你的manifest.xml文件。。 另外,你连接的是局域网还是互联网。 用的是模拟器还是真机? 这效果都不一样的。。
已经添加了 现在已经能访问网络了,谢谢。 可是Json的时候取到的值都是空的,请教一下怎么解决[/quote] 取到的json首先看看字符串是不是空的。 我看你访问的是php,我不知道你的那个php会不会响应请求返回json传。[/quote]我那个php返回是有json数据的 可是取回来的是空的 搜狗的这个地址也是 http://music.sogou.com/sogou_phb/coo/music/getCampMeta2.jsp?cate=randomsong&count=100&jsoncallback=xiexiebangmang[/quote] 是返回的json串为空? 还是json串不为空,但是解析出来的数据为空呢?
teemai 2013-10-23
  • 打赏
  • 举报
回复
引用 19 楼 zzqqdwc 的回复:
[quote=引用 17 楼 AA5279AA 的回复:] [quote=引用 5 楼 zzqqdwc 的回复:] [quote=引用 3 楼 AA5279AA 的回复:] 加了网络权限了,那让我看看你的manifest.xml文件。。 另外,你连接的是局域网还是互联网。 用的是模拟器还是真机? 这效果都不一样的。。
已经添加了 现在已经能访问网络了,谢谢。 可是Json的时候取到的值都是空的,请教一下怎么解决[/quote] 取到的json首先看看字符串是不是空的。 我看你访问的是php,我不知道你的那个php会不会响应请求返回json传。[/quote]我那个php返回是有json数据的 可是取回来的是空的 搜狗的这个地址也是 http://music.sogou.com/sogou_phb/coo/music/getCampMeta2.jsp?cate=randomsong&count=100&jsoncallback=xiexiebangmang[/quote] 你怎么取的?这个肯定可以获取到
饭在桌上 2013-10-23
  • 打赏
  • 举报
回复
引用 17 楼 AA5279AA 的回复:
[quote=引用 5 楼 zzqqdwc 的回复:] [quote=引用 3 楼 AA5279AA 的回复:] 加了网络权限了,那让我看看你的manifest.xml文件。。 另外,你连接的是局域网还是互联网。 用的是模拟器还是真机? 这效果都不一样的。。
已经添加了 现在已经能访问网络了,谢谢。 可是Json的时候取到的值都是空的,请教一下怎么解决[/quote] 取到的json首先看看字符串是不是空的。 我看你访问的是php,我不知道你的那个php会不会响应请求返回json传。[/quote]我那个php返回是有json数据的 可是取回来的是空的 搜狗的这个地址也是 http://music.sogou.com/sogou_phb/coo/music/getCampMeta2.jsp?cate=randomsong&count=100&jsoncallback=xiexiebangmang
饭在桌上 2013-10-23
  • 打赏
  • 举报
回复
引用 16 楼 huxiweng 的回复:
1、用浏览器打开,访问这个url:http://192.168.1.2/2/index.php检查是否有结果。 如果没有结果就看为什么没有结果,这里没有结果的话,那你android请求http://10.0.2.2/2/index.php这个也不会有结果。这和Android没关系了,是后台代码的问题。保证后台代码能正确返回结果,android就没问题了。
我那个php返回是有json数据的 可是取回来的是空的 搜狗的这个地址也是 http://music.sogou.com/sogou_phb/coo/music/getCampMeta2.jsp?cate=randomsong&count=100&jsoncallback=xiexiebangmang
失落夏天 2013-10-23
  • 打赏
  • 举报
回复
引用 5 楼 zzqqdwc 的回复:
[quote=引用 3 楼 AA5279AA 的回复:] 加了网络权限了,那让我看看你的manifest.xml文件。。 另外,你连接的是局域网还是互联网。 用的是模拟器还是真机? 这效果都不一样的。。
已经添加了 现在已经能访问网络了,谢谢。 可是Json的时候取到的值都是空的,请教一下怎么解决[/quote] 取到的json首先看看字符串是不是空的。 我看你访问的是php,我不知道你的那个php会不会响应请求返回json传。
饭在桌上 2013-10-23
  • 打赏
  • 举报
回复
引用 25 楼 AA5279AA 的回复:
[quote=引用 24 楼 zzqqdwc 的回复:] [quote=引用 21 楼 AA5279AA 的回复:] [quote=引用 20 楼 huxiweng 的回复::] 这是主要的解析代码、
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;
	}
}
你解析list的方式貌似不太对。。 给你一个我写的例子吧。。

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]好的 谢谢 我试试!
teemai 2013-10-23
  • 打赏
  • 举报
回复
1、用浏览器打开,访问这个url:http://192.168.1.2/2/index.php检查是否有结果。 如果没有结果就看为什么没有结果,这里没有结果的话,那你android请求http://10.0.2.2/2/index.php这个也不会有结果。这和Android没关系了,是后台代码的问题。保证后台代码能正确返回结果,android就没问题了。
失落夏天 2013-10-23
  • 打赏
  • 举报
回复
引用 24 楼 zzqqdwc 的回复:
[quote=引用 21 楼 AA5279AA 的回复:] [quote=引用 20 楼 huxiweng 的回复::] 这是主要的解析代码、
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;
	}
}
你解析list的方式貌似不太对。。 给你一个我写的例子吧。。

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;
	}
饭在桌上 2013-10-23
  • 打赏
  • 举报
回复
[quote=引用 21 楼 AA5279AA 的回复:] [quote=引用 20 楼 huxiweng 的回复::] 这是主要的解析代码、
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;
	}
}
饭在桌上 2013-10-23
  • 打赏
  • 举报
回复
引用 21 楼 AA5279AA 的回复:
[quote=引用 19 楼 zzqqdwc 的回复:] [quote=引用 17 楼 AA5279AA 的回复:] [quote=引用 5 楼 zzqqdwc 的回复:] [quote=引用 3 楼 AA5279AA 的回复:] 加了网络权限了,那让我看看你的manifest.xml文件。。 另外,你连接的是局域网还是互联网。 用的是模拟器还是真机? 这效果都不一样的。。
已经添加了 现在已经能访问网络了,谢谢。 可是Json的时候取到的值都是空的,请教一下怎么解决[/quote] 取到的json首先看看字符串是不是空的。 我看你访问的是php,我不知道你的那个php会不会响应请求返回json传。[/quote]我那个php返回是有json数据的 可是取回来的是空的 搜狗的这个地址也是 http://music.sogou.com/sogou_phb/coo/music/getCampMeta2.jsp?cate=randomsong&count=100&jsoncallback=xiexiebangmang[/quote] 是返回的json串为空? 还是json串不为空,但是解析出来的数据为空呢?[/quote]解析到的数据为空!
饭在桌上 2013-10-22
  • 打赏
  • 举报
回复
引用 12 楼 huxiweng 的回复:
你直接访问你那个url有结果吗
打印不出数据!
饭在桌上 2013-10-22
  • 打赏
  • 举报
回复
引用 12 楼 huxiweng 的回复:
你直接访问你那个url有结果吗
直接访问 显示无法连接!
饭在桌上 2013-10-22
  • 打赏
  • 举报
回复
teemai 2013-10-22
  • 打赏
  • 举报
回复
你直接访问你那个url有结果吗
teemai 2013-10-22
  • 打赏
  • 举报
回复
引用 10 楼 zzqqdwc 的回复:
引用 9 楼 huxiweng 的回复:
你看result能否打印出来
我试试看。好像我这不能访问那个IP。
那个url要改成你访问的那个10.0.0.2的那个
饭在桌上 2013-10-22
  • 打赏
  • 举报
回复
引用 9 楼 huxiweng 的回复:
你看result能否打印出来
我试试看。好像我这不能访问那个IP。
teemai 2013-10-22
  • 打赏
  • 举报
回复
你看result能否打印出来
teemai 2013-10-22
  • 打赏
  • 举报
回复
引用 7 楼 zzqqdwc 的回复:
引用 6 楼 huxiweng 的回复:
[quote=引用 4 楼 zzqqdwc 的回复:] [quote=引用 2 楼 huxiweng 的回复:] 192.168.1.2是你机器的IP的话就改成上面那个
已经成功了 谢谢 可视Json的时候取到的值都是空的,请教一下怎么解决
检查后台发送的json是否有值 [/quote]有的。能帮我看看源码 或者给一个方法可以吗?谢谢了!源码地址: http://pan.baidu.com/s/1vcEQr[/quote] 给你个参考吧:

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;
    }
    
}

饭在桌上 2013-10-22
  • 打赏
  • 举报
回复
引用 6 楼 huxiweng 的回复:
引用 4 楼 zzqqdwc 的回复:
[quote=引用 2 楼 huxiweng 的回复:] 192.168.1.2是你机器的IP的话就改成上面那个
已经成功了 谢谢 可视Json的时候取到的值都是空的,请教一下怎么解决
检查后台发送的json是否有值 [/quote]有的。能帮我看看源码 或者给一个方法可以吗?谢谢了!源码地址: http://pan.baidu.com/s/1vcEQr
加载更多回复(6)

50,527

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧