Java Android Studio数组值到TextView
我已经按照Wintech的教程建立了到MS SQL数据库的连接,我的结果是在调试器中得到200,因此我知道它正在成功连接。我的问题是,我无法从查询中获得要在应用程序中显示的结果 activity_main.xml:Java Android Studio数组值到TextView,java,android,xml,android-studio,connection,Java,Android,Xml,Android Studio,Connection,我已经按照Wintech的教程建立了到MS SQL数据库的连接,我的结果是在调试器中得到200,因此我知道它正在成功连接。我的问题是,我无法从查询中获得要在应用程序中显示的结果 activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/re
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.pmatthews.appointmentcreator.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="openWebpage"
android:src="@drawable/logo" />
<ScrollView
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="Appointment Number:"
android:textColor="#000000" />
<EditText
android:id="@+id/ApptNumView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1" />
<ImageButton
android:id="@+id/refreshButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/refresh"
android:onClick="testConnection" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="Material Owner Number:"
android:textColor="#000000" />
<TextView
android:id="@+id/MatOwnNumView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_weight="6.5"
android:text="Carrier Number:"
android:textColor="#000000" />
<TextView
android:id="@+id/CarrNumView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_weight="4" />
<ImageButton
android:id="@+id/CarrNumButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/searchicon"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="82dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="Purchase Order Number:"
android:textColor="#000000" />
<EditText
android:id="@+id/PONInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="82dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="Release Number:"
android:textColor="#000000" />
<EditText
android:id="@+id/RelNumInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="Rail Car No:"
android:textColor="#000000" />
<EditText
android:id="@+id/RailCarInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Actuals"
android:textColor="#000000"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/truckArrivedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:text="Truck has Arrived"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="Bill Lading No In:"
android:textColor="#000000" />
<EditText
android:id="@+id/billNumInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="40dp"
android:layout_weight="1"
android:text="Arrival Date:"
android:textColor="#000000" />
<DatePicker
android:id="@+id/arrivalDateInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:datePickerMode="calendar"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="Arrival Time:"
android:textColor="#000000" />
<TimePicker
android:id="@+id/arrivalTimeInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:timePickerMode="spinner" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/recieveButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="Recieve Inventory" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/saveButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:text="Save"
/>
<Button
android:id="@+id/cancelButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<TextView
android:id="@+id/txtConNum"
android:layout_width="fill_parent"
android:layout_height="100dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Android-SQL Server数据库连接(第2部分,获取和发布数据)
您正在替换最后一个读取行输出,因此最后一行可能是\n个字符。
你应该替换
while ((line = reader.readLine()) != null){
status = line;
}
与
编辑
protected String doInBackground(String... connUrl){
HttpURLConnection conn = null;
BufferedReader reader;
StringBuilder sb;
try{
final URL url = new URL(connUrl[0]);
conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("Content-Type", "application/json;
charset=utf-8");
conn.setRequestMethod("GET");
int result = conn.getResponseCode();
if(result == 200){
InputStream in = new BufferedInputStream(conn.getInputStream());
reader = new BufferedReader(new InputStreamReader(in));
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line);
}
}
}catch(Exception ex){
}
return sb.toString();
}
protected void onPostExecute(String result){
super.onPostExecute(result);
System.out.println(result);
}
我仔细地运行了调试器,发现它正在将查询中的数据存储到result
。我在ListView中添加了一个TextView,在AsyncTask
中声明为txtV
,在PostExecute
中添加了txtV.setText(结果)
。。。。。。字符串行=null;while((line=reader.readLine())!=null){sb.append(line);}返回sb.toString();}catch(异常ex){}返回状态。。。。。。仍然没有显示任何结果。。我做错了吗?你能在BufferedReader阅读器正下方的“onPostExecute”中打印结果字符串吗;所以它可以解决我仍然无法解决符号sb。。我肯定是因为我太笨了,对不起
while ((line = reader.readLine()) != null){
sb.append(line);
}
..
return sb.toString()
protected String doInBackground(String... connUrl){
HttpURLConnection conn = null;
BufferedReader reader;
StringBuilder sb;
try{
final URL url = new URL(connUrl[0]);
conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("Content-Type", "application/json;
charset=utf-8");
conn.setRequestMethod("GET");
int result = conn.getResponseCode();
if(result == 200){
InputStream in = new BufferedInputStream(conn.getInputStream());
reader = new BufferedReader(new InputStreamReader(in));
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line);
}
}
}catch(Exception ex){
}
return sb.toString();
}
protected void onPostExecute(String result){
super.onPostExecute(result);
System.out.println(result);
}