Android 台灣中文網
標題:
關於表單的傳送在android應用
[打印本頁]
作者:
hl2dm
時間:
2012-10-26 19:22
標題:
關於表單的傳送在android應用
大家好
我是剛學java沒多久
正在摸索android的初學者
最近剛好在公司上班發現
公司的一台控制器是利用表單來進行控制
也就是說POST出去的資料
(
<form method="post" action="http://xxx.xx.80.132/OPENDOOR">
<p align="right">
<input type="submit" name="DOOR" value="開門">
)
會影響控制器的動作
老闆說這雖然跟你的職務內容無關
但是你可以玩玩看沒關係
在安卓上操作該怎麼做呢?
google了一下看到某鄉民的部落格
根據他的範例:
http://0rz.tw/xPRGe
我更改了private String uriAPI = "http://192.168.1.3/httpPostTest.php";
改為private String uriAPI = "http://xxx.xx.80.132/OPENDOOR";
params.add(new BasicNameValuePair("data", strTxt));
params.add(new BasicNameValuePair("data", strTxt));
改為params.add(new BasicNameValuePair("DOOR", strTxt));
然後進行測試都顯示HOST NAME NOT BE NULL
完全不知道怎麼弄lol
因為我只有基礎的asp跟HTML加上剛學的JAVA概念而已
有沒有版友可以指引我一下的orz
對於傳值根處理回傳值我根本不知道該從何下手
感激不盡
作者:
hl2dm
時間:
2012-10-28 19:45
我有試著找了網路上兩個範例來測試
不過很明顯的都失敗orz
希望版友能給些幫助
這是壹號方法
按鈕按下去之後沒有任何反應
不知道有沒有正確動作
伺服器那邊似乎也沒有紀錄
public class MainActivity extends Activity implements OnClickListener
{
private EditText txtMessage;
private EditText txtMessage2;
private Button sendBtn;
private String uriAPI = "http://119.14.80.132/ACT_ID_1";
/** 「要更新版面」的訊息代碼 */
protected static final int REFRESH_DATA = 0x00000001;
/** 建立UI Thread使用的Handler,來接收其他Thread來的訊息 */
Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
// 顯示網路上抓取的資料
case REFRESH_DATA:
String result = null;
if (msg.obj instanceof String)
result = (String) msg.obj;
if (result != null)
// 印出網路回傳的文字
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMessage = (EditText) findViewById(R.id.editText1);
txtMessage2 = (EditText) findViewById(R.id.editText2);
sendBtn = (Button) findViewById(R.id.button1);
if (sendBtn != null)
sendBtn.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v == sendBtn)
{
if (txtMessage != null)
{
// 擷取文字框上的文字
String msg = txtMessage.getEditableText().toString();
String msg2 = txtMessage2.getEditableText().toString();
// 啟動一個Thread(執行緒),將要傳送的資料放進Runnable中,讓Thread執行
Thread t = new Thread(new sendPostRunnable(msg));
Thread a = new Thread(new sendPostRunnable(msg2));
t.start();
}
}
}
private String sendPostDataToInternet(String strTxt)
{
/* 建立HTTP Post連線 */
HttpPost httpRequest = new HttpPost(uriAPI);
/*
* Post運作傳送變數必須用NameValuePair[]陣列儲存
*/
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "abc"));
params.add(new BasicNameValuePair("pwd", "654321"));
try
{
/* 發出HTTP request */
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/* 取得HTTP response */
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
/* 若狀態碼為200 ok */
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
/* 取出回應字串 */
String strResult = EntityUtils.toString(httpResponse
.getEntity());
// 回傳回應字串
return strResult;
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
class sendPostRunnable implements Runnable
{
String strTxt = null;
String strTxt2 = null;
// 建構子,設定要傳的字串
public sendPostRunnable(String strTxt)
{
this.strTxt = strTxt;
}
@Override
public void run()
{
String result = sendPostDataToInternet(strTxt);
mHandler.obtainMessage(REFRESH_DATA, result).sendToTarget();
Intent open =new Intent();
open.setClass(MainActivity.this,op.class);
startActivity(open);
}
}
}
複製代碼
貳號方法
碰到的問題是
按下送出之後
都被連線拒絕
我查了一下可能是
因為沒有加上 <uses-permission android:name="android.permission.INTERNET" />
但是我加上去之後在按鈕按下的同時程式就會崩潰
public class Main extends Activity implements OnClickListener
{
private EditText txtMessage;
private Button sendBtn;
private String uriAPI = "http://119.14.80.132/ACT_ID_324";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtMessage = (EditText) findViewById(R.id.editText1);
sendBtn = (Button) findViewById(R.id.button1);
if (sendBtn != null)
{
sendBtn.setOnClickListener(this);
}
}
@Override
public void onClick(View v)
{
if (v == sendBtn)
{
String msg = null;
if (txtMessage != null)
{
msg = txtMessage.getEditableText().toString();
String result = sendPostDataToInternet(msg);
if (result != null)
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
}
}
private String sendPostDataToInternet(String strTxt)
{
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("USX0", strTxt));
try
{
/* 發出HTTP request */
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/* 取得HTTP response */
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
/* 若狀態碼為200 ok */
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
/* 取出回應字串 */
String strResult = EntityUtils.toString(httpResponse
.getEntity());
// 回傳回應字串
return strResult;
}
} catch (ClientProtocolException e)
{
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
} catch (IOException e)
{
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
} catch (Exception e)
{
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
}
return null;
}
}
複製代碼
作者:
ploglin
時間:
2012-10-29 12:34
要加上
<uses-permission android:name="android.permission.INTERNET" />
才有權限可以存取網路。
程式異常的話,就把 LogCat 的 Error Message 貼上來看看吧
作者:
hl2dm
時間:
2012-10-29 20:17
我一下班就重回家開始測試了
這是Locat出來的東西
然後在執行到Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT)
debug裝置有幫我停住他
然後我按繼續
跳出這段logcat
10-29 20:08:30.620: I/ActivityManager(1849): Killing proc 20755:com.example.asd/10151: force stop
10-29 20:08:30.880: W/ActivityThread(20966): Application com.example.asd is waiting for the debugger on port 8100...
10-29 20:08:47.360: E/AndroidRuntime(20966): at com.example.asd.MainActivity.sendPostDataToInternet(MainActivity.java:208)
10-29 20:08:47.360: E/AndroidRuntime(20966): at com.example.asd.MainActivity.onClick(MainActivity.java:117)
複製代碼
debug上還會跳出ZygoteInit$MethodAndArgsCaller.run() line: 986
還有一個頁面上面寫Source not found
按鈕上面寫Edit Source Lookup Path
歡迎光臨 Android 台灣中文網 (https://apk.tw/)
Powered by Discuz! X3.1