manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wo.checkdrugdemo">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TableLayout
android:layout_width="match_parent"
android:layout_height="56dp">
<TableRow>
<TextView
android:layout_weight="1"
android:text="国字准号:"
android:layout_height="50dp"
android:textSize="24dp"/>
<EditText
android:id="@+id/inputNum"
android:text="Z20093192"
android:layout_width="240dp"
android:layout_height="50dp"
/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="56dp">
<TableRow>
<TextView android:layout_weight="1"/>
<Button
android:id="@+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="查询"
android:textSize="30dp" />
<TextView android:layout_weight="1"/>
<Button
android:id="@+id/btnExit"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"
android:textSize="30dp"/>
<TextView android:layout_weight="1"/>
</TableRow>
</TableLayout>
<TextView
android:id="@+id/display"
android:textSize="24dp"
android:scrollbars="vertical"
android:scrollbarStyle="outsideOverlay"
android:scrollbarFadeDuration="2000"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Activity文件代码
package com.example.wo.checkdrugdemo;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by yuanlifu on 2016/9/21.
*/
public class MainActivity extends AppCompatActivity {
private Button btnExit;
private Button btnCheck;
private EditText inputText = null;
private TextView display;
static String url = "http://www.tngou.net/api/drug/number?number=";
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
btnExit = (Button) findViewById(R.id.btnExit);
btnCheck = (Button) findViewById(R.id.btnCheck);
inputText = (EditText) findViewById(R.id.inputNum);
display = (TextView) findViewById(R.id.display);
display.setMovementMethod(new ScrollingMovementMethod());
btnCheck.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (inputText.getText().toString()!=null){
try{
processResponse(
searchRequest(inputText.getText().toString())
);
}catch (Exception e){
Log.v("Exception Google search","Exception:"+e.getMessage());
}
}
}
});
btnExit.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
inputText.setText("");
}
});
closeStrictMode();
}
/**
* 据说是如果用android端 使用HttpURLConnection请求,得到的getResponseCode()会返回-1
* 主要问题在于线程, 要单独走一个线程, 不能直接走主线程
* 解决方法有两种:
*一:为该请求单独起一个线程
*二:自己写个方法:
*/
public static void closeStrictMode() {
StrictMode.setThreadPolicy(
new StrictMode
.ThreadPolicy
.Builder()
.detectAll()
.penaltyLog()
.build());
}
public String searchRequest(String searchString) throws IOException {
String newFeed = url+searchString;
StringBuilder response = new StringBuilder();
Log.v("gsearch","gsearch url:"+newFeed);
URL url = new URL(newFeed);
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
httpconn.setReadTimeout(10000);
httpconn.setConnectTimeout(15000);
httpconn.setRequestMethod("GET");
httpconn.setDoInput(true);
httpconn.connect();
if (httpconn.getResponseCode()==200){
BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192);
String strLine = null;
while ((strLine = input.readLine()) != null){
response.append(strLine);
}
input.close();
}
return response.toString();
}
public void processResponse(String resp){
JSONObject root = null;
try {
root = new JSONObject(resp);
String htmlText = new String();
htmlText = root.getString("message");
display.setText(Html.fromHtml(htmlText));
} catch (JSONException e) {
e.printStackTrace();
}
}
}