package com.example.nbwb;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import org.apache.http.client.utils.URLEncodedUtils;
import com.example.util.JsonParpser;
import com.example.util.StringUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class SendwbActivity extends Activity {
private EditText sendwbtext;
private String btext,userid,json;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sendwb);
sendwbtext=(EditText) findViewById(R.id.sendwbtext);
SharedPreferences pres= SendwbActivity.this.getSharedPreferences("user", Context.MODE_PRIVATE);
userid=pres.getString("userid", "");
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
// Toast.makeText(SendwbActivity.this, userid+" "+btext, Toast.LENGTH_SHORT)
// .show();
if("1".equals(json)){
Toast.makeText(SendwbActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
}else if ("0".equals(json)) {
Toast.makeText(SendwbActivity.this, "发送失败", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(SendwbActivity.this, "数据获取错误", Toast.LENGTH_SHORT).show();
}
}
};
}
public void sumbit(View view){
btext=sendwbtext.getText().toString();
// Toast.makeText(this, btext, Toast.LENGTH_SHORT).show();
if ("".equals(btext)||"".equals(userid)) {
Toast.makeText(SendwbActivity.this, "输入不能为空或用户获取错误", Toast.LENGTH_SHORT)
.show();
}else {
new Thread(){
@Override
public void run() {
String url="http://expect-xh.oicp.net:52/NBwb/BlogServlet?function=addBlog&userid="+userid+"&btext="+ URLEncoder.encode(btext);//这里的中文处理
HttpURLConnection con=null;
InputStream is=null;
try {
URL address=new URL(url);
con=(HttpURLConnection)address.openConnection();
con.setConnectTimeout(10000);
is=con.getInputStream();
json=StringUtil.readStream(is);
Log.i("send", json);
Message msg=new Message();
Bundle data=new Bundle();
data.putString("json", json);
msg.setData(data);
handler.sendMessage(msg);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
public void returnbtu(View view){
Intent intent=new Intent(this,NBSendwbActivity.class);
startActivity(intent);
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sendwb, menu);
return true;
}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import org.apache.http.client.utils.URLEncodedUtils;
import com.example.util.JsonParpser;
import com.example.util.StringUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class SendwbActivity extends Activity {
private EditText sendwbtext;
private String btext,userid,json;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sendwb);
sendwbtext=(EditText) findViewById(R.id.sendwbtext);
SharedPreferences pres= SendwbActivity.this.getSharedPreferences("user", Context.MODE_PRIVATE);
userid=pres.getString("userid", "");
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
// Toast.makeText(SendwbActivity.this, userid+" "+btext, Toast.LENGTH_SHORT)
// .show();
if("1".equals(json)){
Toast.makeText(SendwbActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
}else if ("0".equals(json)) {
Toast.makeText(SendwbActivity.this, "发送失败", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(SendwbActivity.this, "数据获取错误", Toast.LENGTH_SHORT).show();
}
}
};
}
public void sumbit(View view){
btext=sendwbtext.getText().toString();
// Toast.makeText(this, btext, Toast.LENGTH_SHORT).show();
if ("".equals(btext)||"".equals(userid)) {
Toast.makeText(SendwbActivity.this, "输入不能为空或用户获取错误", Toast.LENGTH_SHORT)
.show();
}else {
new Thread(){
@Override
public void run() {
String url="http://expect-xh.oicp.net:52/NBwb/BlogServlet?function=addBlog&userid="+userid+"&btext="+ URLEncoder.encode(btext);//这里的中文处理
HttpURLConnection con=null;
InputStream is=null;
try {
URL address=new URL(url);
con=(HttpURLConnection)address.openConnection();
con.setConnectTimeout(10000);
is=con.getInputStream();
json=StringUtil.readStream(is);
Log.i("send", json);
Message msg=new Message();
Bundle data=new Bundle();
data.putString("json", json);
msg.setData(data);
handler.sendMessage(msg);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
public void returnbtu(View view){
Intent intent=new Intent(this,NBSendwbActivity.class);
startActivity(intent);
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sendwb, menu);
return true;
}
}