Java 通过Android使用RESTful web服务编写数据
我正在开发android应用程序。我们的java开发团队为 读取和写入数据。我成功地将数据读取并显示到设备中。我使用以下android代码插入数据,但它不起作用Java 通过Android使用RESTful web服务编写数据,java,android,post,Java,Android,Post,我正在开发android应用程序。我们的java开发团队为 读取和写入数据。我成功地将数据读取并显示到设备中。我使用以下android代码插入数据,但它不起作用 public class MainActivity extends Activity { ProgressDialog pd; List<NameValuePair>list; @Override protected void onCreate(Bundle savedInstanceSt
public class MainActivity extends Activity
{
ProgressDialog pd;
List<NameValuePair>list;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Mytask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class Mytask extends AsyncTask<String,String,String>
{
@Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
Toast.makeText(getBaseContext(), "Inser Record Succesfully", Toast.LENGTH_LONG).show();
pd.dismiss();
}
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
pd=new ProgressDialog(MainActivity.this);
pd.setMessage("Please Wait");
pd.show();
}
@Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
pushdata();
return null;
}
}
public void pushdata()
{
try
{
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost("http://192.168.1.239:8080/KesariRating/rating/add");
// add data to url
list=new ArrayList<NameValuePair>(5);
list.add(new BasicNameValuePair("exce","e"));
list.add(new BasicNameValuePair("vgood","null"));
list.add(new BasicNameValuePair("good","null"));
list.add(new BasicNameValuePair("poor","null"));
list.add(new BasicNameValuePair("fair","null"));
post.setEntity(new UrlEncodedFormEntity(list));
HttpResponse response=client.execute(post);
HttpEntity entity=response.getEntity();
}
catch(Exception e)
{
Log.d("Error in Push","",e);
}
}
}
下面是java代码
public class RatingCore {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String excellent;
private String vgood;
private String good;
private String poor;
private String fair;
private Date timestamp;
public RatingCore() {
}
public RatingCore(int id, String excellent, String vgood, String good,
String poor, String fair, Timestamp timestamp) {
super();
this.id = id;
this.excellent = excellent;
this.vgood = vgood;
this.good = good;
this.poor = poor;
this.fair = fair;
this.timestamp = timestamp;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getExcellent() {
return excellent;
}
public void setExcellent(String excellent) {
this.excellent = excellent;
}
public String getVgood() {
return vgood;
}
public void setVgood(String vgood) {
this.vgood = vgood;
}
public String getGood() {
return good;
}
public void setGood(String good) {
this.good = good;
}
public String getPoor() {
return poor;
}
public void setPoor(String poor) {
this.poor = poor;
}
public String getFair() {
return fair;
}
public void setFair(String fair) {
this.fair = fair;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
}
这是我发送一些数据到服务器数据库的代码,我希望它能帮助您
private void validat_user(String stg1, String stg2,String stg3, String stg4,String stg5, String stg6,String stg7, String stg8,String stg9, String stg10,String stg11)
{
WebServiceTask wst = new WebServiceTask(WebServiceTask.POST_TASK, this, "Authentication in progress...");
wst.addNameValuePair("A_ID", stg1);
wst.addNameValuePair("A_NAME", stg2);
wst.addNameValuePair("A_PWD1", stg3);
wst.addNameValuePair("A_JOIN", stg4);
wst.addNameValuePair("A_END", stg5);
wst.addNameValuePair("A_COURSE", stg6);
wst.addNameValuePair("A_FACID", stg7);
wst.addNameValuePair("A_SESSION", stg8);
wst.addNameValuePair("A_BATCH", stg9);
wst.addNameValuePair("A_BRANCH1", stg10);
wst.addNameValuePair("A_GENDER", stg11);
wst.execute(new String[] { SERVICE_URL });
}
@SuppressWarnings("deprecation")
public void no_net()
{
display( "No Network Connection");
final AlertDialog alertDialog = new AlertDialog.Builder(Staff.this).create();
alertDialog.setTitle("No Internet Connection");
alertDialog.setMessage("You don't have internet connection.\nElse please check the Internet Connection Settings.");
//alertDialog.setIcon(R.drawable.error_info);
alertDialog.setCancelable(false);
alertDialog.setButton("Close", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
alertDialog.cancel();
Staff.this.finish();
System.exit(0);
}
});
alertDialog.setButton2("Use Local DataBase", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
display( "Accessing local DataBase.....");
alertDialog.cancel();
}
});
alertDialog.show();
}
private class WebServiceTask extends AsyncTask<String, Integer, String> {
public static final int POST_TASK = 1;
private static final String TAG = "WebServiceTask";
// connection timeout, in milliseconds (waiting to connect)
private static final int CONN_TIMEOUT = 3000;
// socket timeout, in milliseconds (waiting for data)
private static final int SOCKET_TIMEOUT = 5000;
private int taskType = POST_TASK;
private Context mContext = null;
private String processMessage = "Processing...";
private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
private ProgressDialog pDlg = null;
public WebServiceTask(int taskType, Context mContext, String processMessage) {
this.taskType = taskType;
this.mContext = mContext;
this.processMessage = processMessage;
}
public void addNameValuePair(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}
private void validate_user(字符串stg1、字符串stg2、字符串stg3、字符串stg4、字符串stg5、字符串stg6、字符串stg7、字符串stg8、字符串stg9、字符串stg10、字符串stg11)
{
WebServiceTask wst=新的WebServiceTask(WebServiceTask.POST_任务,这是“正在进行的身份验证…”);
wst.addNameValuePair(“A_ID”,stg1);
wst.addNameValuePair(“A_名称”,stg2);
wst.addNameValuePair(“A_PWD1”,stg3);
wst.addNameValuePair(“A_JOIN”,stg4);
wst.addNameValuePair(“A_端”,stg5);
wst.addNameValuePair(“A_课程”,stg6);
wst.addNameValuePair(“A_FACID”,stg7);
wst.addNameValuePair(“A_会话”,stg8);
wst.addNameValuePair(“一批”,stg9);
wst.addNameValuePair(“A_BRANCH1”,stg10);
wst.addNameValuePair(“A_性别”,stg11);
execute(新字符串[]{SERVICE_URL});
}
@抑制警告(“弃用”)
公共空间无网格()
{
显示(“无网络连接”);
final AlertDialog AlertDialog=新建AlertDialog.Builder(Staff.this.create();
alertDialog.setTitle(“无互联网连接”);
alertDialog.setMessage(“您没有internet连接。\n请检查internet连接设置。”);
//alertDialog.setIcon(R.drawable.error\u info);
alertDialog.setCancelable(假);
alertDialog.setButton(“关闭”,新建DialogInterface.OnClickListener())
{
public void onClick(DialogInterface dialog,int which)
{
alertDialog.cancel();
Staff.this.finish();
系统出口(0);
}
});
alertDialog.setButton2(“使用本地数据库”,新建DialogInterface.OnClickListener())
{
public void onClick(DialogInterface dialog,int which)
{
显示(“访问本地数据库…”;
alertDialog.cancel();
}
});
alertDialog.show();
}
私有类WebServiceTask扩展异步任务{
公共静态最终int POST_任务=1;
私有静态最终字符串TAG=“WebServiceTask”;
//连接超时,以毫秒为单位(等待连接)
专用静态最终内部连接超时=3000;
//套接字超时,以毫秒为单位(等待数据)
专用静态最终int套接字\u超时=5000;
private int taskType=后期任务;
私有上下文mContext=null;
私有字符串processMessage=“正在处理…”;
private ArrayList params=new ArrayList();
private ProgressDialog pDlg=null;
公共WebServiceTask(int任务类型、上下文mContext、字符串processMessage){
this.taskType=taskType;
this.mContext=mContext;
this.processMessage=processMessage;
}
public void addNameValuePair(字符串名称、字符串值){
添加(新的BasicNameValuePair(名称、值));
}
什么不起作用?有错误或异常吗?记录未插入表中。我想知道如何在androidplease help中创建类的对象并使用post方法发送。我遇到了问题。我不会在应用程序中进一步
private void validat_user(String stg1, String stg2,String stg3, String stg4,String stg5, String stg6,String stg7, String stg8,String stg9, String stg10,String stg11)
{
WebServiceTask wst = new WebServiceTask(WebServiceTask.POST_TASK, this, "Authentication in progress...");
wst.addNameValuePair("A_ID", stg1);
wst.addNameValuePair("A_NAME", stg2);
wst.addNameValuePair("A_PWD1", stg3);
wst.addNameValuePair("A_JOIN", stg4);
wst.addNameValuePair("A_END", stg5);
wst.addNameValuePair("A_COURSE", stg6);
wst.addNameValuePair("A_FACID", stg7);
wst.addNameValuePair("A_SESSION", stg8);
wst.addNameValuePair("A_BATCH", stg9);
wst.addNameValuePair("A_BRANCH1", stg10);
wst.addNameValuePair("A_GENDER", stg11);
wst.execute(new String[] { SERVICE_URL });
}
@SuppressWarnings("deprecation")
public void no_net()
{
display( "No Network Connection");
final AlertDialog alertDialog = new AlertDialog.Builder(Staff.this).create();
alertDialog.setTitle("No Internet Connection");
alertDialog.setMessage("You don't have internet connection.\nElse please check the Internet Connection Settings.");
//alertDialog.setIcon(R.drawable.error_info);
alertDialog.setCancelable(false);
alertDialog.setButton("Close", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
alertDialog.cancel();
Staff.this.finish();
System.exit(0);
}
});
alertDialog.setButton2("Use Local DataBase", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
display( "Accessing local DataBase.....");
alertDialog.cancel();
}
});
alertDialog.show();
}
private class WebServiceTask extends AsyncTask<String, Integer, String> {
public static final int POST_TASK = 1;
private static final String TAG = "WebServiceTask";
// connection timeout, in milliseconds (waiting to connect)
private static final int CONN_TIMEOUT = 3000;
// socket timeout, in milliseconds (waiting for data)
private static final int SOCKET_TIMEOUT = 5000;
private int taskType = POST_TASK;
private Context mContext = null;
private String processMessage = "Processing...";
private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
private ProgressDialog pDlg = null;
public WebServiceTask(int taskType, Context mContext, String processMessage) {
this.taskType = taskType;
this.mContext = mContext;
this.processMessage = processMessage;
}
public void addNameValuePair(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}