可能重复:
Android中的调用方法
我正在尝试联系 API。我还没有看到它是否有效……但我的测试信息甚至无效。
这是我的主要活动:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.test.MESSAGE";
@SuppressLint("ParserError")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void sendIP(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
}
这是 DisplayMessageActivity:-“hello”应该写在屏幕上,而不是空白。我想如果那没有出现,那么我的请求可能也没有在任何地方实现,所以关于这方面的建议也会有所帮助。
public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://api.site/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String test = "hello";
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(test);
//TextView myTextView = (TextView) findViewById(R.id.myTextView);
// myTextView.setText(test);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
而且我已经允许在清单中访问互联网!
谢谢!