這個工作適合我。
請儘量到IP地址的本地主機的內部
的Android權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
類RetrieveFeedTask
代碼:
class RetreiveFeedTask extends AsyncTask<String, Void, Void> {
private Exception exception;
protected Void doInBackground(String... datas) {
try {
Socket s = new Socket("10.10.10.46", 8888);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
for (String string : datas) {
dout.writeUTF(string);
}
dout.flush();
dout.close();
s.close();
} catch (Exception e) {
this.exception = e;
}
return null;
}
protected void onPostExecute(Void void1) {
// do nothing;
}
public RetreiveFeedTask() {
}
public Exception getException() {
return exception;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
RetreiveFeedTask feedTask = new RetreiveFeedTask();
feedTask.execute(new String[] { "hello", "hello" });
if (feedTask.getException() != null) {
Log.d(TAG, feedTask.getException().toString());
}
}
發佈您的代碼。 – eightx2
轉到www.google.com並在那裏粘貼您的查詢。你的查詢有很多結果。 –
@SamSam:您可能不會在主線程上執行網絡操作,因爲它會阻塞用戶界面。爲它使用一個單獨的線程。最簡單的方法是使用'AsyncTask'類。 – Codo