我正在为某人开发Android项目,我正在开展一项注册活动,所以我正在尝试使用RestQuery中的方法,但即使我导入了RestQuery,该类也无法识别该方法。谁能帮我这个?我是Android的新手。
这是Register类:
package com.RuMad.goveho;
import android.app.ActionBar;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.os.AsyncTask;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.RuMad.rest.RestQuery;
/**
* Created by chris on 6/22/13.
*/
public class Register extends Activity implements OnClickListener {
ActionBar actionBar;
private Button mRegister;
private EditText uName;
private EditText eM;
private EditText dob;
private EditText zCode;
private EditText pWord;
private EditText cpWord;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle("Register");
mRegister = (Button)findViewById(R.id.button);
mRegister.setOnClickListener(this);
uName = (EditText)findViewById(R.id.username);
eM = (EditText)findViewById(R.id.registerEmail);
dob = (EditText)findViewById(R.id.editText);
zCode = (EditText)findViewById(R.id.zipCode);
pWord = (EditText)findViewById(R.id.password);
cpWord = (EditText)findViewById(R.id.confirmPassword);
}
public void onClick (View v){
String username = uName.getText().toString();
String email = eM.getText().toString();
String birthday = dob.getText().toString();
String zip = zCode.getText().toString();
String password = pWord.getText().toString();
String confirm = cpWord.getText().toString();
boolean invalid = false;
if (username.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Enter your username", Toast.LENGTH_SHORT).show();
}
else
if (email.equals(""))
{
invalid = true;
Toast.makeText((getApplicationContext(), "Enter your email", Toast.LENGTH_SHORT).show();
}
else
if (birthday.equals(""))
{
invalid = true;
Toast.makeText((getApplicationContext(), "Enter your birthday", Toast.LENGTH_SHORT).show();
}
else
if (zip.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Enter your zipcode", Toast.LENGTH_SHORT).show();
}
else
if (password.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(),"Enter your password", Toast.LENGTH_SHORT).show();
}
else
if (!confirm.equals(password))
{
invalid = true;
Toast.makeText(getApplicationContext(),"This does not match your initial password",
Toast.LENGTH_SHORT).show();
}
else
if (invalid == false)
{
registerDevice(email,password,device)
{
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right);
}
}
这是RestQuery:
package com.RuMad.rest;
import android.os.Build;
import android.util.Log;
import com.RuMad.pojos.Device;
import com.RuMad.utils.DateUtil;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Kyle on 6/22/13.
*/
public class RestQuery {
private static String baseUrl = "https://dev.goveho.com";
public static void registerDevice(String email, String password, Device device) {
JSONObject json = new JSONObject();
try {
json.put("email", email);
json.put("password", password);
json.put("device", device.getJsonObject());
Log.d("register params", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "text/json"));
String response = doPost(baseUrl + "/services/security/device", null, se);
Log.d("register response", response);
} catch (JSONException e) {
Log.e("register", "problem setting json values", e);
} catch (UnsupportedEncodingException e) {
Log.e("register", "problem creating string entity", e);
} catch (IOException e) {
Log.e("register", "problem calling register", e);
}
}
public static void login(String email, String password, String deviceId) {
JSONObject json = new JSONObject();
try {
json.put("email", email);
json.put("password", password);
json.put("deviceId", deviceId);
Log.d("login params", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "text/json"));
String response = doPost(baseUrl + "/services/security", null, se);
Log.d("login response", response);
} catch (JSONException e) {
Log.e("login", "problem setting json values", e);
} catch (UnsupportedEncodingException e) {
Log.e("login", "problem creating string entity", e);
} catch (IOException e) {
Log.e("login", "problem calling login", e);
}
}
public static void logout(String sessionToken, String deviceId) {
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
try {
String response = doDelete(baseUrl + "/services/security", headers);
Log.d("logout response", response);
} catch (IOException e) {
Log.e("logout", "problem calling logout", e);
}
}
public static void updateDevice(String sessionToken, String deviceId, Device newDevice) {
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
JSONObject json = new JSONObject();
try {
json.put("device", newDevice.getJsonObject());
Log.d("updateDevice params", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "text/json"));
String response = doPut(baseUrl + "/services/security/device", headers, se);
Log.d("updateDevice response", response);
} catch (JSONException e) {
Log.e("updateDevice", "problem setting json values", e);
} catch (UnsupportedEncodingException e) {
Log.e("updateDevice", "problem creating string entity", e);
} catch (IOException e) {
Log.e("updateDevice", "problem calling updateDevice", e);
}
}
public static void rideSearch(String sessionToken, String deviceId, String departZip,
int departRadius, String destinationZip, int destinationRadius,
long start, long end) {
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
JSONObject json = new JSONObject();
try {
json.put("departureZip", departZip);
json.put("departureRadius", departRadius);
json.put("destinationZip", destinationZip);
json.put("destinationRadius", destinationRadius);
json.put("start", DateUtil.getDateString(start));
json.put("end", DateUtil.getDateString(end));
Log.d("rideSearch params", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "text/json"));
String response = doPost(baseUrl + "/services/ride/search", headers, se);
Log.d("rideSearch response", response);
} catch (JSONException e) {
Log.e("rideSearch", "problem setting json values", e);
} catch (UnsupportedEncodingException e) {
Log.e("rideSearch", "problem creating string entity", e);
} catch (IOException e) {
Log.e("rideSearch", "problem calling rideSearch", e);
}
}
public static void offerRide(String sessionToken, String deviceId, String name,
String start, String destination, long departureTime,
String timeFlex, String repeat, int maxPassengers,
String category, List<Integer> groups, String info,
boolean onlyVisibleToCollege){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
JSONObject json = new JSONObject();
try {
json.put("name", name);
json.put("start", start);
json.put("destination", destination);
json.put("departureTime", DateUtil.getDateString(departureTime));
json.put("timeFlex", timeFlex);
json.put("repeat", repeat);
json.put("maxPassengers", maxPassengers);
json.put("category", category);
if(groups!=null && !groups.isEmpty())
json.put("groups", groups);
json.put("info", info);
json.put("onlyVisibleToCollege", onlyVisibleToCollege);
Log.d("offerRide params", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "text/json"));
String response = doPost(baseUrl + "/services/ride/offer", headers, se);
Log.d("offerRide response", response);
} catch (JSONException e) {
Log.e("offerRide", "problem setting json values", e);
} catch (UnsupportedEncodingException e) {
Log.e("offerRide", "problem creating string entity", e);
} catch (IOException e) {
Log.e("offerRide", "problem calling offerRide", e);
}
}
public static void requestRide(String sessionToken, String deviceId, String name,
String start, String destination, long departureTime,
String timeFlex, String repeat, int maxPassengers,
String category, List<Integer> groups, String info,
boolean onlyVisibleToCollege){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
JSONObject json = new JSONObject();
try {
json.put("name", name);
json.put("start", start);
json.put("destination", destination);
json.put("departureTime", departureTime);
json.put("timeFlex", timeFlex);
json.put("repeat", repeat);
json.put("maxPassengers", maxPassengers);
json.put("category", category);
// if(groups!=null && !groups.isEmpty())
json.put("groups", groups);
json.put("info", info);
json.put("onlyVisibleToCollege", onlyVisibleToCollege);
Log.d("requestRide params", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "text/json"));
String response = doPost(baseUrl + "/services/ride/request", headers, se);
Log.d("requestRide response", response);
} catch (JSONException e) {
Log.e("requestRide", "problem setting json values", e);
} catch (UnsupportedEncodingException e) {
Log.e("requestRide", "problem creating string entity", e);
} catch (IOException e) {
Log.e("requestRide", "problem calling requestRide", e);
}
}
public static void getMessages(String sessionToken, String deviceId){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
try {
String response = doGet(baseUrl + "/services/user/messages/5", headers);
Log.d("getMessages response", response);
} catch (IOException e) {
Log.e("getMessages", "problem calling getMessages", e);
}
}
public static void sendMessage(String sessionToken, String deviceId, String subject, String user, String body){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
JSONObject json = new JSONObject();
try {
json.put("subject", subject);
json.put("user", user);
json.put("body", body);
Log.d("sendMessage params", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "text/json"));
String response = doPost(baseUrl + "/services/user/messages", headers, se);
Log.d("sendMessage response", response);
} catch (JSONException e) {
Log.e("sendMessage", "problem setting json values", e);
} catch (UnsupportedEncodingException e) {
Log.e("sendMessage", "problem creating string entity", e);
} catch (IOException e) {
Log.e("sendMessage", "problem calling sendMessage", e);
}
}
public static void myGroups(String sessionToken, String deviceId){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
try {
String response = doGet(baseUrl + "/services/user/groups", headers);
Log.d("myGroups response", response);
} catch (IOException e) {
Log.e("myGroups", "problem calling getMessages", e);
}
}
public static void createGroup(String sessionToken, String deviceId, String name){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
JSONObject json = new JSONObject();
try {
json.put("name", name);
Log.d("createGroup params", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "text/json"));
String response = doPost(baseUrl + "/services/user/groups", headers, se);
Log.d("createGroup response", response);
} catch (JSONException e) {
Log.e("createGroup", "problem setting json values", e);
} catch (UnsupportedEncodingException e) {
Log.e("createGroup", "problem creating string entity", e);
} catch (IOException e) {
Log.e("createGroup", "problem calling createGroup", e);
}
}
public static void viewProfile(String sessionToken, String deviceId, String username){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
try {
String response = doGet(baseUrl + "/services/user/profile/" + username, headers);
Log.d("viewProfile response", response);
} catch (IOException e) {
Log.e("viewProfile", "problem calling viewProfile", e);
}
}
public static void joinRide(String sessionToken, String deviceId, int rideId){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
JSONObject json = new JSONObject();
try {
json.put("rideId", rideId);
Log.d("joinRide params", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "text/json"));
String response = doPost(baseUrl + "/services/ride/join", headers, se);
Log.d("joinRide response", response);
} catch (JSONException e) {
Log.e("joinRide", "problem setting json values", e);
} catch (UnsupportedEncodingException e) {
Log.e("joinRide", "problem creating string entity", e);
} catch (IOException e) {
Log.e("joinRide", "problem calling joinRide", e);
}
}
public static void leaveRide(String sessionToken, String deviceId, int rideId){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
JSONObject json = new JSONObject();
try {
json.put("rideId", rideId);
Log.d("leaveRide params", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "text/json"));
String response = doPost(baseUrl + "/services/ride/leave", headers, se);
Log.d("leaveRide response", response);
} catch (JSONException e) {
Log.e("leaveRide", "problem setting json values", e);
} catch (UnsupportedEncodingException e) {
Log.e("leaveRide", "problem creating string entity", e);
} catch (IOException e) {
Log.e("leaveRide", "problem calling leaveRide", e);
}
}
public static void myDrivingRides(String sessionToken, String deviceId){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
try {
String response = doGet(baseUrl + "/services/user/rides/driver", headers);
Log.d("myDrivingRides response", response);
} catch (IOException e) {
Log.e("myDrivingRides", "problem calling myDrivingRides", e);
}
}
public static void myPassengerRides(String sessionToken, String deviceId){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
try {
String response = doGet(baseUrl + "/services/user/rides/passenger", headers);
Log.d("myPassengerRides response", response);
} catch (IOException e) {
Log.e("myPassengerRides", "problem calling myPassengerRides", e);
}
}
public static void profileImage(String sessionToken, String deviceId, String username){
List<BasicHeader> headers = new ArrayList<BasicHeader>(2);
headers.add(new BasicHeader("X-GovehoSession", sessionToken));
headers.add(new BasicHeader("X-GovehoMobile", deviceId));
try{
byte[] data = doGetBytes(baseUrl+"/services/user/profile/"+username+"/image", headers);
Log.d("profileImage data", new String(data));
} catch (IOException e) {
Log.e("profileImage", "problem calling profileImage", e);
}
}
private static byte[] doGetBytes(String url, List<BasicHeader> headers) throws IOException {
HttpClient httpClient = HttpClientFactory.getHttpClient();
HttpGet httpGet = new HttpGet(url);
setHeaders(httpGet, headers);
HttpResponse response = httpClient.execute(httpGet);
return getResultBytes(response);
}
private static String doGet(String url, List<BasicHeader> headers) throws IOException {
HttpClient httpClient = HttpClientFactory.getHttpClient();
HttpGet httpGet = new HttpGet(url);
setHeaders(httpGet, headers);
HttpResponse response = httpClient.execute(httpGet);
return getResult(response);
}
private static String doPost(String url, List<BasicHeader> headers, StringEntity params) throws IOException {
HttpClient httpClient = HttpClientFactory.getHttpClient();
HttpPost httpPost = new HttpPost(url);
setHeaders(httpPost, headers);
httpPost.setEntity(params);
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
return getResult(response);
}
private static String doDelete(String url, List<BasicHeader> headers) throws IOException {
HttpClient httpClient = HttpClientFactory.getHttpClient();
HttpDelete httpDelete = new HttpDelete(url);
setHeaders(httpDelete, headers);
HttpResponse response = httpClient.execute(httpDelete);
return getResult(response);
}
private static String doPut(String url, List<BasicHeader> headers, StringEntity params) throws IOException {
HttpClient httpClient = HttpClientFactory.getHttpClient();
HttpPut httpPut = new HttpPut(url);
setHeaders(httpPut, headers);
httpPut.setEntity(params);
HttpResponse response = httpClient.execute(httpPut);
return getResult(response);
}
private static String getResult(HttpResponse response) throws IOException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String jsonString = out.toString();
return jsonString;
} else {
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase() + " code="+statusLine.getStatusCode());
}
}
private static byte[] getResultBytes(HttpResponse response) throws IOException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
byte[] data = out.toByteArray();
return data;
} else {
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase() + " code="+statusLine.getStatusCode());
}
}
private static void setHeaders(AbstractHttpMessage message, List<BasicHeader> headers) {
if (headers != null) {
for (BasicHeader header : headers) {
message.addHeader(header);
}
}
}
}
我尝试使用的方法是RegisterDevice方法。另外,如果有人有时间回答这个问题,我该如何检索RegisterDevice方法的Device对象信息?
答案 0 :(得分:2)
这行代码
registerDevice(email,password,device)
应该是
RestQuery.registerDevice(email,password,device)
您导入了它,但仍需要按名称引用它。