//首頁
package com.example.post;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()
                .penaltyLog()
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedClosableObjects()
        .penaltyLog()
        .penaltyDeath()
        .build());
        //寫載入資料且顯示畫面
        Button b1=(Button)findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String r = DBstring.DB1("select * from text1");
                TextView TV=(TextView)findViewById(R.id.textView);
                TV.setText(r.toString());
                int intV = Integer.valueOf(String TV);
                if (intV>0){
                    TextView TV2=(TextView)findViewById(R.id.textView2);
                    TV2.setText("正");
                }
                else{
                    TextView TV2=(TextView)findViewById(R.id.textView2);
                    TV2.setText("負");
                }
            }
        });
    }
}
package com.example.post;
import android.util.Log;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
//做連結的地方
public class DBstring {
    public static String DB1(String i){
        String result="";
        try {
            HttpClient HC=new DefaultHttpClient();
            HttpPost HP =new HttpPost("網址");
            ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();//ArrayList是一個陣列表,NameValuePair是一個接口
            params.add(new BasicNameValuePair("S1",i));//BasicNameValuePair先將參數放入list,再對參數進行URL編碼S1為網頁設定POST的名稱
            HP.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//UrlEncodedFormEntity為指定編碼參數構造一個新的來發送HttpPost請求(ex:UTF8)
            HttpResponse HR=HC.execute(HP);//HttpResponse=接收傳回的值。execute為執行=>用在GET and POST
            result= EntityUtils.toString(HR.getEntity(),HTTP.UTF_8);//取得回傳的資料實體(需自行轉UTF8格式)轉成靜態(EntityUtils)字串
        }catch (Exception e){
            Log.i("錯誤訊息",e.toString());//Log.i 發送日誌消息並記錄異常
        }
        return result;
    }
}
問題在MainActivity39行
int intV = Integer.valueOf(String TV);錯誤
TV->id名稱textView的textview
TV.setText(r.toString());
//連結網址的php資料放入textview裡,textview目前顯示資料為15
PHP資料能正常顯示
但想把TV字串的資料轉換成整數
拿去做判斷式.......
int intV = Integer.valueOf(String TV);//沒法修改成數值
應該如何修改才能把TV字串帶入Integer.valueOf轉換成數值呢???
OS:測試過 PHP資料能夠顯示
英文字母轉成數字??您這數字指的是什麼?
順序?    (a=1 , b=2 ,c=3...... z=26)
ascii?   (a = 十進制97 = 十六進制 61)
長度?    ("TV".length() = 2)
數字字串轉整數?
我猜您是要取TextView TV 內的數字?
把int intV = Integer.valueOf("TV");換成int intV = Integer.parseInt(TV.getText().toString().trim());看看
沒有寫清楚不好意思,真正要問的是
int intV = Integer.valueOf(裡面放入TV字串);框框內應該怎麼寫
TV.setText(r.toString());
//連結網址的php資料放入textview裡,textview目前顯示資料為15
php的資料丟入if...else判斷
要用int intV = Integer.valueOf();把字串"15"轉換成數值15
com.example.post.MainActivity$1.onClick(MainActivity.java:38)
程式仍然會跳出
以為要用php輸出的資料需要用到json格式
去掉陣列格式
再用上大大改的文法有成功
int intV = Integer.parseInt(TV.getText().toString().trim());
這次有成功執行了,非常感謝