台灣最大程式設計社群網站
線上人數
1210
 
會員總數:243332
討論主題:188103
歡迎您免費加入會員
討論區列表 >> Android APP 開發 >> 介面按鈕無法呼叫PHP檔
[]  
[我要回覆]
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
介面按鈕無法呼叫PHP檔
價值 : 50 QP  點閱數:204 回應數:0

樓主

RMine
門外漢
0 1
9 0
發送站內信

下面是目前測試檔,一共做了5個按鍵,分別對應button1(aaa)、button2(bbb)等以此類推,但目前只有"即時"按鍵可以正常抓取數值(如下圖),而電壓按鍵卻會連同資料表格一起顯示如:"name=val2,value=110,TIME=2018/XX/XX 00:00:00"無法像下圖正常顯示數值。
電流、溫度、history按鍵也皆無法做使用,燒入手機時皆正常無錯誤訊息

[hr]

public class MainActivity extends AppCompatActivity {
private int contentView;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private TextView tv;
private TextView tv1;
private TextView tv2;
private Button mbtn1,mbtn2,mbtn3,mbtn4,mbtn5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView2);
tv1 = (TextView) findViewById(R.id.textView3);
tv2 = (TextView) findViewById(R.id.textView4);
mbtn1=(Button)findViewById(R.id.button) ;
mbtn2=(Button)findViewById(R.id.button2) ;
mbtn3=(Button)findViewById(R.id.button3) ;
mbtn4=(Button)findViewById(R.id.button4) ;
mbtn5=(Button)findViewById(R.id.button5) ;


//aa();

mbtn1.setOnClickListener(btn1);
mbtn2.setOnClickListener(btn1);
mbtn3.setOnClickListener(btn1);
mbtn4.setOnClickListener(btn1);
mbtn5.setOnClickListener(btn1);

}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
try {
String jasonText = msg.getData().getString("msg"); // response 會錯誤的
Log.i("datar",jasonText);
tv.setText(jasonText);
String AA = new JSONArray(jasonText).getJSONObject(0).getString("name");
String BB = new JSONArray(jasonText).getJSONObject(0).getString("value");
tv.setText("電壓"+"\n"+BB);
tv2.setText(jasonText);
String AA1= new JSONArray(jasonText).getJSONObject(1).getString("name");
String BB1= new JSONArray(jasonText).getJSONObject(1).getString("value");
tv2.setText("溫度"+"\n"+BB1);
tv1.setText(jasonText);
String AA2 = new JSONArray(jasonText).getJSONObject(2).getString("name");
String BB2 = new JSONArray(jasonText).getJSONObject(2).getString("value");
tv1.setText("電流"+"\n"+BB2);
}
catch (Exception e){
e.printStackTrace();
}
}
};

private void aa(){

Thread thread = new Thread(new Runnable() {
@Override
public void run() {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://200.1.1.36/aaa.php?data=123");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
String strMessage = "";
while (data != -1){
char current = (char) data;
strMessage += String.valueOf(current);
data = isw.read();
}
Log.i("String = ", strMessage);
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("msg", strMessage);
message.setData(bundle);
handler.sendMessage(message);
}catch (Exception e){
e.printStackTrace();
}finally {
if (urlConnection != null){
urlConnection.disconnect();
}
}
}
});thread.start();
}
private void aaa(){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://200.1.1.36/aaa.php?data=123")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("error",Log.getStackTraceString(e));
}

@Override
public void onResponse(Call call, Response response) throws IOException {
//Log.i("data",response.body().string());
try {
String jasonText = response.body().string(); // response 會錯誤的
Log.i("datar",jasonText);
/*tv.setText(jasonText);

String AA = new JSONArray(jasonText).getJSONObject(0).getString("name");
Log.i("datar",AA);
String BB = new JSONArray(jasonText).getJSONObject(0).getString("value");
tv.setText("電壓"+AA+"\n"+BB);
tv2.setText(jasonText);
String AA1= new JSONArray(jasonText).getJSONObject(1).getString("name");
String BB1= new JSONArray(jasonText).getJSONObject(1).getString("value");
tv2.setText("溫度"+"\n"+BB1);
tv1.setText(jasonText);
String AA2 = new JSONArray(jasonText).getJSONObject(2).getString("name");
String BB2 = new JSONArray(jasonText).getJSONObject(2).getString("value");
tv1.setText("電流"+"\n"+BB2);*/

Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("msg", jasonText);
message.setData(bundle);
handler.sendMessage(message);
}
catch (Exception e){
e.printStackTrace();
}
}
});
}

private void bbb(){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://200.1.1.36/bbb.php?data=123")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("error",Log.getStackTraceString(e));
}

@Override
public void onResponse(Call call, Response response) throws IOException {
//Log.i("data",response.body().string());
try {
String jasonText = response.body().string(); // response 會錯誤的
Log.i("datar",jasonText);
tv.setText(jasonText);
//String AA = new JSONArray(jasonText).getJSONObject(0).getString("value");
String BB = new JSONArray(jasonText).getJSONObject(0).getString("value");
tv.setText("電壓"+"\n"+BB);
//tv2.setText(jasonText);
//String AA1= new JSONArray(jasonText).getJSONObject(1).getString("name");
//String BB1= new JSONArray(jasonText).getJSONObject(1).getString("value");
//tv2.setText("溫度"+"\n"+BB1);
//tv1.setText(jasonText);
String AA2 = new JSONArray(jasonText).getJSONObject(2).getString("name");
String BB2 = new JSONArray(jasonText).getJSONObject(2).getString("value");
//tv1.setText("電流"+"\n"+BB2);

/* Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("msg", jasonText);
message.setData(bundle);
handler.sendMessage(message);*/
}
catch (Exception e){
e.printStackTrace();
}
}
});
}


private void ccc(){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://200.1.1.36/ccc.php?data=123")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("error",Log.getStackTraceString(e));
}

@Override
public void onResponse(Call call, Response response) throws IOException {
//Log.i("data",response.body().string());
try {
String jasonText = response.body().string(); // response 會錯誤的
Log.i("datar",jasonText);
tv.setText(jasonText);
String AA = new JSONArray(jasonText).getJSONObject(0).getString("name");
String BB = new JSONArray(jasonText).getJSONObject(0).getString("value");
tv.setText("電壓"+"\n"+BB);
tv2.setText(jasonText);
String AA1= new JSONArray(jasonText).getJSONObject(1).getString("name");
String BB1= new JSONArray(jasonText).getJSONObject(1).getString("value");
tv2.setText("溫度"+"\n"+BB1);
tv1.setText(jasonText);
String AA2 = new JSONArray(jasonText).getJSONObject(2).getString("name");
String BB2 = new JSONArray(jasonText).getJSONObject(2).getString("value");
tv1.setText("電流"+"\n"+BB2);
}
catch (Exception e){
e.printStackTrace();
}
}
});
}

private void ddd(){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://200.1.1.36/ddd.php?data=123")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("error",Log.getStackTraceString(e));
}

@Override
public void onResponse(Call call, Response response) throws IOException {
//Log.i("data",response.body().string());
try {
String jasonText = response.body().string(); // response 會錯誤的
Log.i("datar",jasonText);
tv.setText(jasonText);
String AA = new JSONArray(jasonText).getJSONObject(0).getString("name");
String BB = new JSONArray(jasonText).getJSONObject(0).getString("value");
tv.setText("電壓"+"\n"+BB);
tv2.setText(jasonText);
String AA1= new JSONArray(jasonText).getJSONObject(1).getString("name");
String BB1= new JSONArray(jasonText).getJSONObject(1).getString("value");
tv2.setText("溫度"+"\n"+BB1);
tv1.setText(jasonText);
String AA2 = new JSONArray(jasonText).getJSONObject(2).getString("name");
String BB2 = new JSONArray(jasonText).getJSONObject(2).getString("value");
tv1.setText("電流"+"\n"+BB2);
}
catch (Exception e){
e.printStackTrace();
}
}
});
}

private void eee(){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://200.1.1.36/eee.php?data=123")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("error",Log.getStackTraceString(e));
}

@Override
public void onResponse(Call call, Response response) throws IOException {
//Log.i("data",response.body().string());
try {
String jasonText = response.body().string(); // response 會錯誤的
Log.i("datar",jasonText);
tv.setText(jasonText);
String AA = new JSONArray(jasonText).getJSONObject(0).getString("name");
String BB = new JSONArray(jasonText).getJSONObject(0).getString("value");
tv.setText("電壓"+"\n"+BB);
tv2.setText(jasonText);
String AA1= new JSONArray(jasonText).getJSONObject(1).getString("name");
String BB1= new JSONArray(jasonText).getJSONObject(1).getString("value");
tv2.setText("溫度"+"\n"+BB1);
tv1.setText(jasonText);
String AA2 = new JSONArray(jasonText).getJSONObject(2).getString("name");
String BB2 = new JSONArray(jasonText).getJSONObject(2).getString("value");
tv1.setText("電流"+"\n"+BB2);
}
catch (Exception e){
e.printStackTrace();
}
}
});
}


private View.OnClickListener btn1=new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case (R.id.button):
aaa();
break;
case (R.id.button2):
bbb();
break;
case (R.id.button3):
ccc();
break;
case (R.id.button4):
ddd();
break;
case (R.id.button5):
eee();
break;
}
}
};
}

搜尋相關Tags的文章: [ APP ] ,
本篇文章發表於2018-06-11 22:58
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
目前尚無任何回覆
   

回覆
如要回應,請先登入.