这对我有用。
请尝试使用 localhost 中的 IP 地址
安卓权限:
<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());
}
}