我想为用户发送到firebase数据库的每条消息存储public class MyApp {
public static void main(String[] args) throws IOException {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
Credentials credentials = new UsernamePasswordCredentials("user@example.com", "secret");
credsProvider.setCredentials(AuthScope.ANY, credentials);
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(new BasicCookieStore());
context.setCredentialsProvider(credsProvider);
CloseableHttpClient client = HttpClientBuilder.create()
// my server requires this header otherwise it returns response with code 500
.setDefaultHeaders(Collections.singleton(new BasicHeader("x-requested-with", "XMLHttpRequest")))
.setTargetAuthenticationStrategy(new MyAuthStrategy())
.build();
String url = "https://example.com/some/resource";
String url2 = "https://example.com/another/resource";
// ======= REQUEST 1 =======
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request, context);
String responseText = EntityUtils.toString(response.getEntity());
request.reset();
// ======= REQUEST 2 =======
HttpGet request2 = new HttpGet(url);
HttpResponse response2 = client.execute(request2, context);
String responseText2 = EntityUtils.toString(response2.getEntity());
request2.reset();
// ======= REQUEST 3 =======
HttpGet request3 = new HttpGet(url2);
HttpResponse response3 = client.execute(request3, context);
String responseText3 = EntityUtils.toString(response3.getEntity());
request3.reset();
client.close();
}
}
。这是正确的方法吗?
在我的代码中,我使用了以下内容。
MyAuthScheme.authenticate()
当我尝试运行我的应用时,我收到此错误。
Time Stamp
这是我的模型类,Message message = new Message(null, mUserName, downloadUrl.toString(),ServerValue.TIMESTAMP);
messagesDatabaseReference.push().setValue(message);
:
com.google.firebase.database.DatabaseException: Expected a Map while deserializing, but got a class java.lang.Long
答案 0 :(得分:1)
我假设您从数据库中检索timestamp
时收到错误消息。规则是,当您设置timestamp
时,将其设置为Map
,当您检索时,您将检索为Long
。所以为了使它正确,我建议你使用这种方法:
public static String getTimeDate(long timeStamp){
try{
DateFormat dateFormat = getDateTimeInstance();
Date netDate = (new Date(timeStamp));
return dateFormat.format(netDate);
}
catch(Exception e){
return "date";
}
}
如果您只想检索日期,请使用以下方法:
static String getDate(long timeStamp){
try{
DateFormat dateFormat = getDateInstance();
Date netDate = (new Date(timeStamp));
return dateFormat.format(netDate);
}
catch(Exception e){
return "date";
}
}
当您检索timestamp
时,将侦听器放在特定节点上并按照以下方式迭代地图:
if (entry.getKey().equals("timeStamp")) {
Long timeStampLong = (Long) entry.getValue();
String timeStamp = getDate(timeStampLong);
((TextView )findViewById(R.id.time_stamp)).setText(timeStamp);
}
希望它有所帮助。
答案 1 :(得分:0)
我已将模型类Message
改建为:
public class Message {
private String text;
private String name;
private String photoUrl;
private String dateAndTime;
public Message(String text, String name, String photoUrl, String dateAndTime){
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
this.dateAndTime = dateAndTime;
}
// getters and setters
}
在MainActivity
中,我实施了以下内容,将时间戳和用户的消息发送到Firebase数据库。有效!
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM dd hh:mm a");
String dateAndTime = formatter.format(date);
Message message = new Message(mMessageEditText.getText().toString(), mUserName, null, dateAndTime);
messagesDatabaseReference.push().setValue(message);