public class GetActivity extends Activity {
private EditText input;
private Button get;
private Button post;
private Button search;
private TextView result;
final static String phoneUrl="http://www.096.me/api.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get);
search=(Button)findViewById(R.id.serch);
get=(Button)findViewById(R.id.get);
post=(Button)findViewById(R.id.post);
input=(EditText)findViewById(R.id.edit_input);
result=(TextView)findViewById(R.id.result);
get.setOnClickListener(new OnclickListenerImp1());
post.setOnClickListener(new OnclickListenerImp1());
search.setOnClickListener(new OnclickListenerImp1());
}
class OnclickListenerImp1 implements OnClickListener{
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.serch:
break;
case R.id.get:
Get_url();
break;
case R.id.post:
Post_url();
break;
default:
break;
}
}
private void Get_url(){
String phonenum=input.getText().toString();
//phonenum=phonenum.replace(" ", "%20");
try{
//URL geturl=new URL(phoneUrl+"?code="+phonenum+"&output=xml");
URL geturl=new URL(phoneUrl+"?phone="+phonenum+"&mode=xml");
HttpURLConnection httpconn=(HttpURLConnection)geturl.openConnection();
httpconn.setReadTimeout(100000);
if(httpconn.getResponseCode()==HttpURLConnection.HTTP_OK){
Toast.makeText(getApplicationContext(), "get连接成功", Toast.LENGTH_SHORT).show();
InputStreamReader isr=new InputStreamReader(httpconn.getInputStream(),"utf-8");
int i;
String content="";
while((i=isr.read())!=-1){
content=content+(char)i;
}
isr.close();
result.setText(content);
}
httpconn.disconnect();
}catch(Exception e){
Toast.makeText(getApplicationContext(), "get连接失败", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
代码如上
AndroidMainFest.xml 中也添加了internet权限
不知道为什么一直不行
请教各位
------解决方案--------------------
调试看看
InputStreamReader isr=new InputStreamReader(httpconn.getInputStream(),"utf-8");
int i;
String content="";
这个走到没啊。
------解决方案--------------------
httpconn.getResponseCode() 多少啊。
Exception e) 这里面 详细错误信息看看
------解决方案--------------------
你debug 看看啊。。logcat 也可以。
------解决方案--------------------
你debug 的时候 。看看 e.printStackTrace();