Skip to content

Commit

Permalink
hzuapps#6 hzuapps#90 第六次作业
Browse files Browse the repository at this point in the history
  • Loading branch information
xiwu zhong committed Jun 16, 2017
1 parent d21cebf commit 4343316
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.List;

import edu.hzuapps.androidlabs.R;
import edu.hzuapps.androidlabs.homworks.net1414080903136.db.ExpressDB;

public class Net1414080903136Activity extends FragmentActivity {
public static String url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import android.widget.TextView;

import edu.hzuapps.androidlabs.R;
import edu.hzuapps.androidlabs.homworks.net1414080903136.db.ExpressDB;
import edu.hzuapps.androidlabs.homworks.net1414080903136.db.ExpressDao;

import static edu.hzuapps.androidlabs.homworks.net1414080903136.Net1414080903136Activity.bt;

Expand All @@ -22,35 +20,10 @@
*/

public class Net1414080903136ExpressInquiry extends Fragment{

ExpressDB expressDB;
private Button queryBt;
private EditText comName;
private EditText exNum;
private ExpressDao expressDao;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.tab1_net1414080903136,container,false);
queryBt= (Button) view.findViewById(R.id.bt);
comName= (EditText) view.findViewById(R.id.et1);
exNum=(EditText) view.findViewById(R.id.et2);
expressDao=new ExpressDao(getContext());
queryBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//测试数据
String companyName=comName.getText().toString();
String expressNum=exNum.getText().toString();
String date="2017-5-18";
String content="惠州学院快递服务站正在第1次派件 电话:15766844117 请保持电话畅通、耐心等待";
String state="派件";
expressDao.insert(companyName,expressNum,date,content,state);
}
});
return view;


return inflater.inflate(R.layout.tab1_net1414080903136,container,false);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package edu.hzuapps.androidlabs.homeworksrks.net1414080903136;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import edu.hzuapps.androidlabs.R;

public class Net1414080903136ShowJsonActivity extends AppCompatActivity {
TextView tv;

Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String a= parseJson((String) msg.obj);
tv.setText(a);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net1414080903136_show_json);
tv= (TextView) findViewById(R.id.tv_json);
new Thread(){
@Override
public void run() {
super.run();
String a=a("https://raw.githubusercontent.com/zhongxiwu/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903136/1414080903136.json");
Message msg=handler.obtainMessage();
msg.obj=a;
handler.sendMessage(msg);
}
}.start();
}

public String a(String u) {
try {
URL url = new URL(u);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
StringBuilder sb = new StringBuilder();
String a;
int len;
while ((len=is.read(b)) != -1) {
a=new String(b,0,len);
sb.append(a);
}
return sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

public String parseJson(String data){
StringBuilder sb=new StringBuilder();
try {
JSONArray jsonArray=new JSONArray(data);
for (int i=0;i<jsonArray.length();i++){
JSONObject object=jsonArray.getJSONObject(i);
sb.append(object.getString("id"));
sb.append("\n\n");

sb.append(object.getString("state"));
}
} catch (JSONException e) {
e.printStackTrace();
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_net1414080903136_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="edu.hzuapps.androidlabs.homworks.net1414080903136.Net1414080903136MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv_json"/>
</RelativeLayout>

0 comments on commit 4343316

Please sign in to comment.